Visual C++ Debugging: How do I evaluate the time difference between two events?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1

Thread: Visual C++ Debugging: How do I evaluate the time difference between two events?

Threaded View

  1. #1
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Visual C++ Debugging: How do I evaluate the time difference between two events?

    Q: How do I evaluate the time difference between two events?

    A: Accurate time difference between events cannot be evaluated by clocking the time using 'CTime::GetCurrentTime()' or by using a timer. First method is limited in its accuracy to around a second and second method has limitations of lower priority.

    Here I present a function which returns the time elapsed from the last call. First call will always return a zero value. This uses the high-resolution performance counter. If it does not exist, each call will return zero.


    Code:
    float TimeDiff()
    {
      // Last counter reading
      static LARGE_INTEGER OldCounter = {0, 0};
    
      LARGE_INTEGER Counter, Frequency;
      if (QueryPerformanceFrequency(&Frequency))
      {
        // Gets current counter reading
        QueryPerformanceCounter(&Counter);
    
        // Calculates time difference (zero if called first time)
        float TimeDiff = OldCounter.LowPart ? (float) (Counter.LowPart - OldCounterLowPart) / Frequency.LowPart : 0;
    
        // Resets last counter reading
        OldCounter = Counter;
    
        // Returns time difference
        return TimeDiff;
      }
      else
      {
        // No high resolution performance counter; returns zero
        return 0;
      }
    }
    Time difference is returned in seconds and the accuracy is determined by the high-resolution performance counter frequency.



    Last edited by Andreas Masur; April 2nd, 2006 at 04:25 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center