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

  • Thread starter Victor Frankenstein
  • Start date
In summary, the original poster was trying to find a way to count milliseconds and under. He was using a clock_t structure to calculate a function's call time. He tried counting loop time using ASM for a project some time ago, and wasn't very accurate. He eventually found a Windows library which could be used to measure loop time accurately. He also tried running the program in while booted dos mode, and if that were accurate it would be accurate below 0.1 seconds. He also tried porting the library to any C compiler.
  • #1
Victor Frankenstein
29
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
  • #2
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?
 
  • #3
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:
  • #4
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 ?
 
  • #5
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:
  • #6
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.
 
  • #7
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 ?
 
  • #8
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:
  • #9
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:

1. What is the purpose of using clock_t start and stop in C?

The clock_t start and stop functions in C are used to measure the execution time of a specific code or function. This allows programmers to optimize their code and improve its efficiency.

2. How do I use clock_t start and stop in my code?

To use clock_t start and stop in your code, you must first declare a variable of type clock_t. Then, use the clock() function to set the start time and end time of your code. Finally, subtract the start time from the end time to get the total execution time.

3. Can clock_t start and stop be used to measure the execution time of multiple functions?

Yes, clock_t start and stop can be used to measure the execution time of multiple functions. Simply use the clock() function to set the start and end times for each function and calculate the total execution time for all functions.

4. Are there any limitations to using clock_t start and stop?

There are some limitations to using clock_t start and stop. These functions do not take into account any time spent waiting for input or output operations to complete. Additionally, the precision of the clock() function may vary depending on the system it is running on.

5. Are there any alternatives to using clock_t start and stop?

Yes, there are alternative methods for measuring execution time in C, such as using the time() function or implementing your own timing mechanism using high-resolution timers. However, clock_t start and stop are commonly used and provide a simple and reliable way to measure execution time.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
5
Views
879
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
Replies
9
Views
1K
Back
Top