Thread: measuring time in ms/us/ns

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    2

    Question measuring time in ms/us/ns

    I have been looking for methods of measuring time in C programming.

    With time.h, I can only measure in sec., which is useless in most cases.

    I was then told about using ftime(), as demonstrated in this link:

    http://www.phanderson.com/C/ftime.html

    I am able to measure in ms with that function and I'm generally happy enough with it.

    But then if I want to measure more accurately, in clock cycles with a programmed frequency, such that I maximize the performance of the processor, I was told to use QueryPerformanceCounter as described in:

    http://support.microsoft.com/kb/q172338/#appliesto

    with this function, I have trouble understanding. First of all, I don't think this is even C, or is it? Secondly, how do I use it in C code.

    Or are there other ways to achieve the same task, using C?

    I'd appreciate any help

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    BOOL QueryPerformanceCounter(LARGE_INTEGER* count) is a Windows API function. This can be used but is only applicable to windows. Just call it before and after the piece you want timed and subtract one reading from the other. Im sure we have covered this many times. Did you do a search of the boards?
    Last edited by Stoned_Coder; 04-29-2005 at 01:46 PM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What could be difficult about obtaining pairs of numbers and subtracting them?

    > First of all, I don't think this is even C
    Well no, that M$ article is in VB.
    But the names are good, and you have pretty much the same API available to C as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    ftime() is unix, and you should use gettimeofday() instead

    Also consider setitimer() and getitimer() - depnding on your hardware this will have finer grained resolution.
    -- setitimer man page discussing minimum resolution:

    Time values smaller than the resolution of the system clock are
    rounded up to this resolution. The machine-dependent clock resolution
    is 1/HZ seconds, where the constant HZ is defined in <sys/param.h>.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM