I would like to measure time in C, and I am having a tough time figuring it out, all I want is something like this:
- start a timer
- run a method
- stop the timer
- report the time taken (at least to micro accuracy)
Any help would be appreciated.
(I am compiling in windows using mingw)
Take a lok at this one however if you want precise calculation I think you have to use specific libraries on your operating system.
The following is a group of versatile C functions for timer management based on the gettimeofday() system call. All the timer properties are contained in a single ticktimer struct - the interval you want, the total running time since the timer initialization, a pointer to the desired callback you want to call, the number of times the callback was called. A callback function would look like this:
To initialize and start a timer, call ticktimer_init(your_timer, interval, TICKTIMER_RUN, your_timer_cb, 0).
In the main loop of your program call ticktimer_tick(your_timer) and it will decide whether the appropriate amount of time has passed to invoke the callback.
To stop a timer, just call ticktimer_ctl(your_timer, TICKTIMER_STOP).
ticktimer.h:
ticktimer.c:
On Linux you can use
clock_gettime()
:Here's a header file I wrote to do some simple performance profiling (using manual timers):
The
ztime()
function is the main logic you need — it gets the current time and stores it in a 64bit uint measured in microseconds. You can then later do simple math to find out the elapsed time.The
ZenTimer*()
functions are just helper functions to take a pointer to a simple timer struct,ztimer_t
, which records the start time and the end time. TheZenTimerPause()
/ZenTimerResume()
functions allow you to, well, pause and resume the timer in case you want to print out some debugging information that you don't want timed, for example.You can find a copy of the original header file at http://www.gnome.org/~fejj/code/zentimer.h in the off chance that I messed up the html escaping of <'s or something. It's licensed under MIT/X11 so feel free to copy it into any project you do.
Here's a solution for GNU/Linux that uses the x86 CPU timestamp counter:
rdtsc.c:
compile and run with
It reads the correct divisor for your CPU from /proc/cpuinfo and shows how long it took to read that in microseconds, as well as how long it takes to execute sleep(1) in milliseconds.
... Assuming the Mhz rating in /proc/cpuinfo always contains 3 decimal places :-o
High resolution timers that provide a resolution of 1 microsecond are system-specific, so you will have to use different methods to achieve this on different OS platforms. You may be interested in checking out the following article, which implements a cross-platform C++ timer class based on the functions described below:
Windows
The Windows API provides extremely high resolution timer functions:
QueryPerformanceCounter()
, which returns the current elapsed ticks, andQueryPerformanceFrequency()
, which returns the number of ticks per second.Example:
Linux, Unix, and Mac
For Unix or Linux based system, you can use
gettimeofday()
. This function is declared in "sys/time.h".Example:
Note that the above examples need to be compiled with C++, which mingw supports.