Measuring time accurately

This is a discussion on Measuring time accurately within the C++ Programming forums, part of the General Programming Boards category; I am working on a project and i am trying to work this out.I have an event that occurs every ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Measuring time accurately

    I am working on a project and i am trying to work this out.I have an event that occurs every 16.12356 milliseconds.The problem i have is that i need to use that time to count up to 1 second . so it takes 16.12356 * 62.0210425 to reach a second.The problem is how to measure the .0210425 because i can only measure my timw in whole numbers

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,751
    Well which OS/Compiler are you using?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    If the timer granularity does not divide evenly into one second, then you can't use such a timer to precisely count one second. That's just a mathematical fact.

    However, if you are counting seconds over time (i.e. you want to trigger some event every second), it is possible to avoid accumulation of error. Although each specific timeout will not occur exactly on the second, it won't "drift" over time, so long as you are careful about it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by brewbuck View Post
    If the timer granularity does not divide evenly into one second, then you can't use such a timer to precisely count one second. That's just a mathematical fact.

    However, if you are counting seconds over time (i.e. you want to trigger some event every second), it is possible to avoid accumulation of error. Although each specific timeout will not occur exactly on the second, it won't "drift" over time, so long as you are careful about it.
    That's a good point. So you'd be off, on average, by about half the granularity.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Measuring Run Time
    By BIOS in forum C Programming
    Replies: 29
    Last Post: 09-13-2011, 03:07 PM
  2. measuring time in ms/us/ns
    By wanwan in forum C Programming
    Replies: 3
    Last Post: 04-29-2005, 02:42 PM
  3. measuring time of execution
    By KevBin in forum C Programming
    Replies: 6
    Last Post: 12-02-2004, 02:08 PM
  4. Measuring time
    By BruceLeroy in forum C++ Programming
    Replies: 20
    Last Post: 10-07-2004, 03:17 PM
  5. Measuring time of a reply
    By Zewu in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2003, 12:37 PM

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