Counter (Time)

This is a discussion on Counter (Time) within the C Programming forums, part of the General Programming Boards category; Originally Posted by Meldreth except you don't know what the resolution is. you can get the number of clock ticks ...

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.


    --
    Mats
    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
    Posts
    1,485
    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.

Page 2 of 2 FirstFirst 12
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 Cprogramming.com
    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, 05:00 PM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 09:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21