Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Clock_t start, stop in C

  1. Aug 12, 2005 #1
    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 ?
  2. jcsd
  3. Aug 12, 2005 #2


    User Avatar
    Homework Helper

    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?
  4. Aug 12, 2005 #3


    User Avatar
    Staff Emeritus

    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:

    Last edited: Aug 12, 2005
  5. Aug 13, 2005 #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 ?
  6. Aug 13, 2005 #5


    User Avatar
    Staff Emeritus

    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: Aug 13, 2005
  7. Aug 13, 2005 #6
    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.
  8. Aug 16, 2005 #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 ?
  9. Aug 16, 2005 #8


    User Avatar
    Staff Emeritus

    Based on this:


    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:


    Here is a tutorial for using the windows api:

  10. Aug 16, 2005 #9
    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Clock_t start, stop in C
  1. Why not stop ? (Replies: 9)

  2. Starting a forum (Replies: 20)

  3. Stop The Keyblogger! (Replies: 6)