C/C++ Millisecond timers?

If you use RDTSC make sure you use SetProcessAffinityMask too. If you don't do this on a multiprocessor system you will run in big trouble.
 
Demirug said:
If you use RDTSC make sure you use SetProcessAffinityMask too. If you don't do this on a multiprocessor system you will run in big trouble.
You mean Win32 can actually move a running thread to another processor at any unforeseen time? :oops:
 
zeckensack said:
Demirug said:
If you use RDTSC make sure you use SetProcessAffinityMask too. If you don't do this on a multiprocessor system you will run in big trouble.
You mean Win32 can actually move a running thread to another processor at any unforeseen time? :oops:

Not at any time but after each thread switch your thread can run on another processor. If you have a thread switch between your start and end time the RDTSC method can give you very strange results like a negative execution time. I have see this.
 
On UNIX systems you can also do the following :

1.) Fairly portable, fall back to gettimeofday() if it's unavailable :
Code:
#include <time.h>
...
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);

2.) Solaris also supports :
Code:
#include <time.h>
#include <sys/time.h>
...
/* number of nanoseconds since some arbitrary point in the past,
    unaffected by changes to the system time */
struct timespec ts;
clock_gettime(CLOCK_HIGHRES, &ts);
hrtime_t t;
t = gethrtime();

2.) More recent versions of Linux support the following clocks as well:
Code:
/*
       CLOCK_MONOTONIC
              Clock  that  cannot  be  set and represents monotonic time since
              some unspecified starting point.

       CLOCK_PROCESS_CPUTIME_ID
              High-resolution per-process timer from the CPU.

       CLOCK_THREAD_CPUTIME_ID
              Thread-specific CPU-time clock.
*/
clock_gettime(CLOCK_MONOTONIC, &ts);
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
...
 
Back
Top