Thread: stopwatch with time.h possible to be more precise?

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    stopwatch with time.h possible to be more precise?

    Hello,

    I am trying to build a simple "stopwatch" timer in C, to be used as a Rubik's Cube timer.
    I currently am using time.h, setting up start and stop time_t variables, and the enter key to start and stop the timer, giving the result as the difference between the two. I can post the code but I don't think that's necessary, because it works fine.

    My question is how I can make it more precise. It currently only works to the nearest second, when I would like to have it to the nearest millisecond.

    Everything I read tells me the most precise C can get is a second, but I find this hard to believe. Does anyone know of a way to do this or can point me in the right direction?

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    Bryanosaurus,

    It has been my experience that measuring time with the C time libraries is highly compiler implementation specific. I just answered another individual with a timer to the nanosecond...what is diffences betweent link1.cpp and link2.cpp. According to the man pages and header files one can adjust the resolution to the millisecond if it is desired. I have used it for pools of threads on several occasions.

    I just don't have a compiler handy for it at the moment, but it works great when it's implemented. Let me know if this helps, the code segment with the time measuring variables is #8 (at the bottom), and I included information about the libraries to which one must link. The post originally pertained to the performance advantages of automatic allocation versus dynamic allocation.

    Best Regards,

    New Ink -- Henry

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    More accurate timing functions are dependent on the OS you are developing for.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    Quote Originally Posted by Bubba View Post
    More accurate timing functions are dependent on the OS you are developing for.
    If it helps, I am developing on Linux with, obviously, gcc compiler.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    If it's Linux, then gettimeofday() will nominally give you microsecond precision. In reality the precision is significantly less than that, but probably still precise enough for your purposes. It is certainly more precise than a user striking the Enter key, which probably has a jitter of up to a quarter of a second, so it should suffice.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You can look into <timeb.h> functions... There is a struct someplace that includes milliseconds.
    Unfortunately even though the resolution is milliseconds, values jump by whatever the tick rate is - was it around 17 per second from the old DOS days? Could be anything.

    Still, that may give you sufficient resolution for your needs. At least another 0.1 second of accuracy.

  7. #7
    Registered User
    Join Date
    Jul 2010
    Location
    Oklahoma
    Posts
    107
    I concur with Brewbuck. After I did some rooting around in the code library, I updated the source's post: what is diffences betweent link1.cpp and link2.cpp. There was no need to link to an external library either.

    Best Regards,

    New Ink -- Henry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. time.h & Delay
    By gavra in forum C Programming
    Replies: 27
    Last Post: 07-29-2008, 08:53 AM
  2. time.h
    By zensatori in forum C Programming
    Replies: 28
    Last Post: 04-15-2007, 06:39 PM
  3. Stopwatch program (need help!)
    By modnar in forum C Programming
    Replies: 9
    Last Post: 03-22-2004, 12:42 AM
  4. time.h and today's date
    By !NSAN!TY in forum C Programming
    Replies: 3
    Last Post: 10-16-2002, 06:44 PM
  5. Time.h
    By GeekSeb in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2002, 07:54 PM