Help: How to get system time in C?

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
Hi All,
I'm running a pretty long algorithm and would like to know how long each iteration of it takes. What I would like to do is make a stats file printing the system time of when the algorithm begins, and right after it finishes. What I would like to know is, what C command will give me the system time down to the smallest fraction of a second?
Thanks for the info!
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
If you're looking for very high resolution, then you'll have to use OS-dependent calls. Otherwise, you can use time() and difftime().
 

uCsDNerd

Senior member
Mar 3, 2001
338
0
0
Thanks for the response. I am looking for high resolution (down to micro or nano seconds if possible). Do you or anyone else know of such functions for Windows2k and also for Redhat 7.1 (i'm using both OS's for my experiment). Thanks again!
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
In Windows NT there is a call GetTickCount().
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. If you need a higher resolution timer, use amultimedia timer or a high-resolution timer.

High-Resolution Timer
A counter is a general term used in programming to refer to an incrementing variable. Some systems include a high-resolution performance counter that provides high-resolution elapsed times.

If a high-resolution performance counter exists on the system, the QueryPerformanceFrequency function can be used to express the frequency, in counts per second. The value of the count is processor dependent. On some processors, for example, the count might be the cycle rate of the processor clock.

The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter (if one exists on the system). By calling this function at the beginning and end of a section of code, an application essentially uses the counter as a high-resolution timer. For example, suppose that QueryPerformanceFrequency indicates that the frequency of the high-resolution performance counter is 50,000 counts per second. If the application calls QueryPerformanceCounter immediately before and immediately after the section of code to be timed, the counter values might be 1500 counts and 3500 counts, respectively. These values would indicate that .04 seconds (2000 counts) elapsed while the code executed.

Beyond this you would have to get a kernal modification to Windows
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Using QueryPerformanceCounter() and QueryPerformanceFrequency() will provide a very fine resolution (~1 ms) on a Windows NT machine.