How can I accurately count milliseconds using clock_t start and stop in C?

  • Thread starter Thread starter Victor Frankenstein
  • Start date Start date
AI Thread Summary
To accurately count milliseconds or less in C, using the `clock_t` structure may not suffice due to its resolution limitations, particularly on Windows where the default timing resolution is around 10ms. Instead, utilizing a real-time clock (RTC) library is recommended for better precision; on Linux, this can be achieved with `rtc.h`, while Windows users should explore the Windows API for timing functions. The discussion highlights that timing accuracy can be affected by system layers and other running processes, making direct hardware clock access preferable for microsecond-level timing. The original poster's method of timing a loop by dividing total time by iterations may yield inaccurate results due to these factors. Overall, accessing hardware timers or using appropriate system libraries is essential for precise timing in C programming.
Victor Frankenstein
Messages
29
Reaction score
0
In this program I am using I first declare clock_t start, stop then later position start and stop in a loop. To count how long a switch is turned on. The smallest value I see outputed is no lower than 0.05 sec but I want to be able to count miliseconds.

printf("pulse length is %f seconds\n", (stop - start) / CLK_TCK);

What function or code should I use to be able to count lengths miliseconds and under ?
 
Computer science news on Phys.org
the clock_t structure is used to calculate a function's call time. Maybe it's just that it uses 0.05 seconds to perform. Where do you set the start and stop variables?
 
Your probably using windows and the default resolution is something like 10ms, but in linux there is a library called rtc.h which is a real time clock and has a garanteed resolution of something in the order of micro seconds. You'll have to do a google search to see if windows has some type of rtc library.

[edit] Found it for windows:

http://www.decompile.com/html/windows_timer_api.html
 
Last edited by a moderator:
Im using turboC so I don't have that fancy rtc.h library.

What about counting how many seconds a longer {loop(containing var=var+1) while var<1,000,000} will take using clock_t and then... dividing by the number of seconds for the entire sequence by 1,000,000 to get the number of seconds it takes EACH loop, do you think that this method will this give me an accurate value ?
 
What type of accuracy do you want? I guess you could do it that way, but it won't be very accurate. I tried counting loop time using ASM for a project some time ago using the standard way but wasn't very accurate compared to using the onboard RTC.

The "Best Way"(R) is to use your systems RTC library. If your using windows you'll use the windows api, which has nothing to do with rtc.h. I would try exactly the given example above. If you using Linux the actual include statement would be #include <linux/rtc.h>.
 
Last edited:
No, it won't be accurate. Programs running under windows run in a layer so, you have the hardware layer with a real time clock then you have a core OS layer then a GUI layer then your user program layer. Since user programs are not running at the lowest layer the lower layers can preempt your program. So, a loop may take 10uS to execute on november 21 2005 but may take 31 uS 3 days later because you are doing something or a new program is running that wasn't running before.

You need to access a hardware clock directly for accurate timing in the microsecond range... Here, take a look at this: http://www.lochan.org/2005/keith-cl/useful/win32time.html

Good luck.
 
What about running the program in while booted dos mode, would it be accurate it that case ?

How would I use a windows api with turboC, are there any other methods for counting microseconds in turboC ?
 
Based on this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemtimeasfiletime.asp

Just include Windows.h and call the function void GetSystemTimeAsFileTime(
LPFILETIME lpSystemTimeAsFileTime
);

where lpSystemTimeAsFileTime is a pointer to a FILETIME structure to receive the current system date and time in UTC format.

Here is the Filetime structure:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/filetime_str.asp

Here is a tutorial for using the windows api:

http://www.winprog.org/tutorial/
 
Last edited by a moderator:
dduardo said:
Based on this:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getsystemtimeasfiletime.asp

Just include Windows.h and call the function void GetSystemTimeAsFileTime(
LPFILETIME lpSystemTimeAsFileTime
);

where lpSystemTimeAsFileTime is a pointer to a FILETIME structure to receive the current system date and time in UTC format.

Here is the Filetime structure:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/filetime_str.asp

Here is a tutorial for using the windows api:

http://www.winprog.org/tutorial/

That's the Windows OS time though which is not that accurate. Also, is that accurate below 0.1 seconds? The original poster was worried about the lowest time increment being 0.05.

Victor, the link in my previous response has a zip file which contains routines which give access to hardware time events. It shouldn't be too hard to port that to any C compiler. I'm a unix/linux user and have been MS free for some time now except for the stuff I do at work so I'm not speaking from a platform of absolute certainty. Good luck.
 
Last edited by a moderator:
Back
Top