We help IT Professionals succeed at work.

Time measurements

q_low asked
I want to measure exceution time in a readers/writers problem. Is there any library that can aid me in this?

Watch Question

What platform, Compiler? There's no high resolution timing defined by the standard library.

On Windows, use QueryPerformanceCounter/QueryPerformanceFrequency. Under DOS you can either read the timer chip manually (could dig something out). If you can rely on a later pentium, you can use the RDSC instruction (Read Task State Counter, basically, processor ticks).

sorry, no experience with the other side of the coin.


ANSI C has the following functions that can measure time in <time.h>
  time_t time( time_t *timer );
  double difftime( time_t timer1, time_t timer0 );
  clock_t clock( void );

For higher precision you'll have to use the OS APIs.  Win32 gives you (depending on the needed precision):
  QueryPerformanceCounter, QueryPerformanceFrequency


This is for Unix/Linux system.
Use time(), clock() and difftime() for low resolution timing.

For better resolution, use ftime() or gettimeofday().

Really high resolution timing on x86 (some 486/K6 and above) can be implemented using the TSC.  See:


To check if your processor supports the instruction:

% grep mmx /proc/cpuinfo
flags           : fpu vme de pse tsc msr 6 mce cx8 sep mtrr pge 14 cmov ...

Look for "tsc", it means time stamp counter.

In windows API , there is also GetTickCount().
From MSDN:

The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.

the instruction you were talking about has the mnemonic RDTSC (ReaD Time Stamp Counter). If you want to use it you will have to clear the TSD-bit (Time Stamp Disable) in register CR4 -- if it is set, RDTSC can only be executed at privilege level 0. If you are at privilege level 0 (e.g. when creating a device driver) you can also use RDMSR (ReaD from Model-Specific Register) to read the number of clock cycles since the last reset.
Btw. this is all OS-independent.

Didn't mean to be picky -- I'm just passionate about my job, can't help it.
np ;)

Explore More ContentExplore courses, solutions, and other research materials related to this topic.