Clock_t start, stop in C

In this program im 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 ?
 

mezarashi

Homework Helper
651
0
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?
 

dduardo

Staff Emeritus
1,872
2
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 [Broken]
 
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 ?
 

dduardo

Staff Emeritus
1,872
2
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:
690
0
No, it wont 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 ?
 

dduardo

Staff Emeritus
1,872
2
Based on this:

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

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 [Broken]

Here is a tutorial for using the windows api:

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

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

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 [Broken]

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:

Want to reply to this thread?

"Clock_t start, stop in C" You must log in or register to reply here.

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving

Top Threads

Top