count execution time using clock() in milliseconds

This is a discussion on count execution time using clock() in milliseconds within the C++ Programming forums, part of the General Programming Boards category; i have implemented an algorithm and the execution time is really small and when I use clock() I get a ...

  1. #1
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    count execution time using clock() in milliseconds

    i have implemented an algorithm and the execution time is really small and when I use clock() I get a 0 as a result

    is it possible to make clock() display results in milliseconds or even smaller like microseconds?

    if yes, how is this possible? what parameters should I change?

    all I do is

    Code:
    time_t before, after;
    
    before = clock();
    
    algorithm
    
    after = clock();
    
    time <- (double)(after-before)/CLOCKS_PER_SEC
    i need to gather statistical data in seconds in order to see how my implementation reacts on different inputs
    Last edited by nik; 03-31-2011 at 12:25 PM.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    If you know the time in seconds, then surely the time in milliseconds is 1000 times that. Right?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    nik
    nik is offline
    Registered User
    Join Date
    Nov 2010
    Posts
    44
    Quote Originally Posted by brewbuck View Post
    If you know the time in seconds, then surely the time in milliseconds is 1000 times that. Right?
    the problem is that i dont know the time in seconds, it always returns 0

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by nik View Post
    the problem is that i dont know the time in seconds, it always returns 0
    Then the code you are trying to time is taking less than a single clock tick. That means clock() isn't going to be a good way of timing it.

    On UNIX, you can use gettimeofday() to get better resolution. On Windows, you can use QueryPerformanceCounter().
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    There is no standard way to do this. You need to use the functions mentioned or an external library.
    I never put signature, but I decided to make an exception.

  6. #6
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Let me assure you it is possible to get really accurate time measurements (even more than microsecond resolution). I used them many times at a job I was working over the summer. You need to research tsc clocks and high frequency timers.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Quote Originally Posted by Mozza314 View Post
    Let me assure you it is possible to get really accurate time measurements (even more than microsecond resolution). I used them many times at a job I was working over the summer. You need to research tsc clocks and high frequency timers.
    Not on most MS OS systems, except CE.

    Most of the time you will be +/- 50 msec, getting anywhere near a msec requires a filter driver (or realtime OS).

    Both the methods you mention are unreliable, especially on multi core / variable speed CPUs.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help fast
    By blackice in forum C Programming
    Replies: 2
    Last Post: 01-31-2011, 01:19 PM
  2. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  3. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  4. Execution Time in Microseconds?
    By thetinman in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2007, 02:32 PM
  5. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 07:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21