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

  • Thread starter Thread starter Victor Frankenstein
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around accurately measuring time intervals in milliseconds or less using the `clock_t` structure in C programming. Participants explore various methods and libraries available across different operating systems, including Windows and Linux, and consider the limitations of timing accuracy in different environments.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant notes that the output from their timing code is no lower than 0.05 seconds and seeks advice on achieving millisecond accuracy.
  • Another participant suggests that the `clock_t` structure may have limitations, possibly due to the resolution of the system's timer.
  • A participant mentions that Windows has a default timer resolution of around 10ms, while Linux can use `rtc.h` for higher precision.
  • One user inquires about using `clock_t` to measure the time taken for a loop and dividing it by the number of iterations to estimate time per iteration, questioning its accuracy.
  • Another participant argues that this method may not yield accurate results due to the layers of the operating system that can affect timing.
  • Discussion includes the suggestion to access hardware clocks directly for more precise timing, with references to external resources for Windows timing APIs.
  • One participant questions the feasibility of running the program in DOS mode for better accuracy and asks about using Windows APIs with TurboC.
  • Several participants provide links to documentation and tutorials for using Windows APIs to access system time, while also expressing concerns about the accuracy of these methods below 0.1 seconds.
  • There is mention of routines that provide access to hardware time events, with a suggestion that they could be adapted for use in various C compilers.

Areas of Agreement / Disagreement

Participants express differing views on the accuracy of various timing methods, with no consensus on the best approach. Some advocate for using hardware clocks for precision, while others suggest using system APIs, acknowledging limitations in accuracy.

Contextual Notes

Limitations include the dependency on operating system behavior, potential inaccuracies due to multitasking environments, and the resolution of the timers used in different systems. The discussion does not resolve these issues.

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:

Similar threads

Replies
5
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 66 ·
3
Replies
66
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
3K