Thread: Accurate time measurement

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Question Accurate time measurement

    I am running a program with a loop, and this loop is dependent on the speed of the computer or whatever. i.e.
    Code:
    while(1)
    {
    }
    and I want to know when .25 of a second (250 milliseconds) has passed, without using sleep. Using the return value of time(NULL); is hopeless as that seems to count in seconds. Any suggestions?

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    GetTickCount() from windows.h would be one solution.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Include windows.h and use GetTickCount(). This returns the number of milliseconds since windows started. I don't think there is a more precise timer in pure DOS than seconds, as you mentioned.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Great, I'm doing this in Windows API so that should be perfect. I have a look. Thanks.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    18 mS resolution with Sindows???

    I could be wrong... I'm at work and all my books are at home... If your code works ignore this!!!!

    1- I seem to remember that (with MS windows) the tick only gets updated every 18mS. So, even though the result is in milliseconds, it does not have 1mS resolution.

    2- Your in a multitasking envoronment. What happens if the OS is off doing something else at the moment your program wants to check the time? Don't you have to do "something" to make sure your program has 100% control of the CPU during this critical time?

    We have an EPROM/FLASH programmer here that runs from a DOS program. When I asked the manufacturer why there was no Windows software, they said "because we can't control the timing". Their windows product has a clock built into the hardware. (In fact, we run the thing in a DOS window we don't have any problems.)

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yeah MS should really stay out of the atomic clock business!

    Here's a simple busy loop by the way:


    Code:
    void wait( unsigned int time_out )
    {
     time_t finish = GetTickCount()+time_out;
     while(GetTickCount() < finish) continue;
    }

    And a very unscientific way to control speed of execution:


    Code:
    /* pass 0 into function to get you're cpu's 'constant', otherwise, pass your 'constant' in to calculate an approximate ratio of user's cpu speed to the speed you compiled at...*/
     
    double cpu_factor(double constant, short num_averages = 1)
    {
     time_t start;
     double end_time;
     double average = 0;
     short loops;
     int i;
    
         if(num_averages == 0)
          num_averages = 1;
    
         for(loops = 0; loops < num_averages; ++loops)
       {
         i = 0;
         start = clock();
    
            while(++i <= 10000000)
             continue;
    
         end_time = (double)( clock()- start );
    
         average += end_time;
       }
    
      average /= loops;
    
          if(constant == 0) 
        {
          return average;
        }
          else return average/constant;
    }

    Example:


    Code:
    const double my_cpu = 260; /* compiled cpu_factor() */
    double this_cpu;
    
    int main()
    {
      this_cpu = cpu_factor(my_cpu);
    }
    Last edited by Sebastiani; 02-01-2003 at 02:08 AM.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird Times I'm getting
    By afflictedd2 in forum Linux Programming
    Replies: 8
    Last Post: 07-23-2008, 07:18 AM
  2. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  3. Time measurement and sound
    By mewatC in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 01:50 PM
  4. calculating user time and time elapsed
    By Neildadon in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2003, 06:00 PM