Thread: Problem with time count in ms

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    60

    Problem with time count in ms

    Hi,
    I am using the clock() function to count the execution time of a function in ms (as the CLOCKS_PER_SEC is defined to be 1000).
    Unfortunately, this scale of counting doesn't seem to be sensitive enough in my case,
    as the running time of the function as counted by clock() is zero.

    So i am wondering if there is a way to get a more accurate measurement.
    I use the mingw compiler under Windows XP ( i wonder if there is a more accurate function defined in windows.h or something..)

    Any help is appreciated..

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Hmm... 0 is a meaningful result. It tells you your function took less than a millisecond (on your CLOCKS_PER_SEC implementation) to complete.

    I'm unaware of anything more precise than clock ticks per second in C++ in terms of time measurement.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    For a short-lived program, it's like trying to time a bullet with a sundial.

    QueryPerformanceCounter() is the win32 API you seek.
    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
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    http://www.virtualdub.org/blog/pivot/entry.php?id=106

    have a look at this
    http://msdn2.microsoft.com/en-us/library/ms712704.aspx

    if precision timing is important for your you may consider switching to windows vista. as i've heard MS added support for hpet into it

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Call the function N times, where N is large, then divide the time by N.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Running the function multiple times is most often the RIGHT thing to do. [I've written an "auto adjust the number of calls" function a few times, just to figure out how many times the loop needs to be run to get a "solid" number, say 1-3 second]

    QueryPerformanceCounter should be the ticket, as adviced.

    You can ALSO use "timestamp counter", which reads the number of clock-cycles in the processor as a 64-bit number.

    something like this:
    Code:
    __int64 readtsc(void) {
        __int64   tsc;
        __asm {  rdtsc
                        mov  dword ptr tsc, eax
                        mov  dword ptr tsc+4, edx }
    
        return tsc;
    }
    This assumes a microsoft compiler that supports inline assembler. I beleive there are intrinsic functions to do this in MS compiler too - but it's not exactly a "portable" solution.

    By the way, whilst the CLOCKS_PER_SEC is 1000 (for example), it doesn't mean that the counter is updated every millisecond - it may well happen that it's updated every 10 or 100 ms, by adding 10 or 100 every so often, rather than adding 1 every millisecond - in fact, I'm pretty sure older versions of windows had a 50 or 100Hz timer frequency, and that is the timer that clocks is updated by.

    --
    Mats

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    60
    Thanks for all the answers..

    Indeed, the running time of my function is surprisingly small.. I have implemented a minHeap in a pointer machine mode and I was amazed when i realized that it takes only 11 counter clocks (at a frequency of 3579545 Hz) to insert an element in the Heap while it already contains 1.000.000 elements while the process was running at Normal priority (Under Win XP)..

    Anyway, I am currently studying the asymptotic bounding of each function in practice, so i don't actually need to measure the accurate running time, since i don't really care about the coefficients.

    Running the function multiple times seems a good idea, but since i use pointer arithmetic, arguments actually change their values so i would have to switch again to the last value before i call the function again (like deleting a Node after inserting it to the Heap)

    Using several similar objects might be a solution but then again, because of the reduced memory in my system, additional IO processes would affect the measurements..

    So, for now, i choose the QueryPerformanceCounter way..

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    another option would be gettimeofday() in <sys/time.h>. It hides OS-specific implementation detail and offers a uniform interface across platforms. Its precision is dependent on the OS, however. On Linux its accuracy is higher than 1e-5 second if my memory serves me well, but I don't know about windows.

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I came across an article on CodeGuru that defines some macros using the Time Stamp Method matsp described. http://www.codeguru.com/cpp/misc/mis...cle.php/c3895/

    It requires an intel Pentium to work so is not very portable but it measures the cycles using CPUID to serialize the instructions.

    Code:
    #ifdef _DEBUG
    #define PERF_DECLARE         \
      __int64 MSRB, MSRE;        \
      void *mrsb = &MSRB;    \
      void *mrse = &MSRE;    \
      char perfmtrbuf[100];
    
    #define PERF_START          \
      {_asm mov eax, 0          \
      _asm cpuid                \
      _asm rdtsc                \
      _asm mov ebx, mrsb        \
      _asm mov dword ptr [ebx], eax    \
      _asm mov dword ptr [ebx+4], edx  \
      _asm mov eax, 0           \
      _asm cpuid}
    
    #define PERF_STOP           \
      {_asm mov eax, 0          \
      _asm cpuid                \
      _asm rdtsc                \
      _asm mov ebx, mrse        \
      _asm mov dword ptr [ebx], eax   \
      _asm mov dword ptr [ebx+4], edx \
      _asm mov eax, 0           \
      _asm cpuid}
    
    #define PERF_REPORT          \
      {_ui64toa(MSRE-MSRB, perfmtrbuf, 10);    \
      printf("Cycles needed: &#37;s\n", perfmtrbuf);}
    #else
    #define PERF_DECLARE
    #define PERF_START
    #define PERF_STOP
    #define PERF_REPORT
    #endif
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Ping
    By ZakkWylde969 in forum Tech Board
    Replies: 5
    Last Post: 09-23-2003, 12:28 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM