Thread: Counter (Time)

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    Quote Originally Posted by Meldreth View Post
    except you don't know what the resolution is. you can get the number of clock ticks in a second but a tick isn't defined.
    There is a macro that defines CLOCKS_PER_SEC, which can be used to turn the result of clock() into seconds. Note that this may not reflect the actual resolution, e.g. CLOCKS_PER_SEC may be 1000, but it may count up 14 clock-ticks each time, because it is running on a 700-odd Hz timer.

    If you want reasonable portability, then the right thing to do is to write a wrapper function that hides the actual implementation, e.g. a "starttime()" and "stoptime()" that return a floating point value (double) in seconds. By returning a floating point value, there is no reason to worry about how the internal representation of time is done.

    You can then choose the best function for each architecture to solve the problem in the best way on that OS.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Registered User
    Join Date
    Jan 2009
    Thanks guys, Interesting. I have looked up a mac os/unix? equivalent which seems to be mach_absolute_time(). The procedure looks very similar, get ticks, then convert using library functions to get sec, minutes etc. that are independent of the speed of the cpu.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers
    By Big_0_72 in forum C Programming
    Replies: 3
    Last Post: 10-28-2008, 07:51 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. The new FAQ
    By Hammer in forum A Brief History of
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM