Jump to content
  • Advertisement
Sign in to follow this  
Krisc

[.net] Counting elapsed time inaccuracies...

This topic is 4627 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a complex gaming loop setup... In essence the rendering manager calls a callback interface and passes the elapsedtime as a parameter. Eventually this amount of time gets passed into a gravity simulation class. Inside this class the time is added to a running total, when it reaches a certain value, it updates all the velocities. The problem is that the elapsed time is highly innaccurate. You can clearly see that before it counts to 0.25 seconds, it has already been about 5 seconds real time. I can upload my code... but it is very complex in how it is setup. Here is the timing code... I am using C#/DirectX9/.NET2
public class iPerfTimer
{
    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceCounter(out long lpPerformanceCount);

    [DllImport("Kernel32.dll")]
    private static extern bool QueryPerformanceFrequency(out long lpFrequency);

    private long m_startTime;
    private long m_stopTime;

    private double m_duration;

    private bool m_started;

    private long m_freq;

    public iPerfTimer()
    {
        m_startTime = 0;
        m_stopTime = 0;

        if (QueryPerformanceFrequency(out m_freq) == false)
        {
            throw new Win32Exception("High Performance counter not supported!");
        }
    }

    public void Start()
    {
        Thread.Sleep(0);

        QueryPerformanceCounter(out m_startTime);

        m_started = true;
    }

    public void Stop()
    {
        QueryPerformanceCounter(out m_stopTime);

        m_duration = (double)(m_stopTime - m_startTime) / (double)m_freq;

        m_started = false;
    }

    public double Duration
    {
        get
        {
            return m_duration;
        }
    }

    public bool Started
    {
        get
        {
            return m_started;
        }
    }
}

Share this post


Link to post
Share on other sites
Advertisement
the .NET 2.0 framework has a pretty cool highres timer built in called the Stopwatch class which is built into the System.Diagnostics namespace. You can check out its info here. I know it won't tell you why your code doesn't work, but its a far cleaner way of doing timing than importing .dll's and possibly doing some unsafe stuff. Hope this helps.

Share this post


Link to post
Share on other sites
QueryPerformanceCounter depends on the processor frequency, so if you have a processor that changes its frequency based on heat or performance (most newer processors), it could mess you up.

I'd agree that the .Net 2.0 Stopwatch is a good idea.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!