C# TimeSpan.FromTicks() inaccurate?
Asked Answered
M

3

6

I have been using C# for some time now to make a small game, and while testing said game on a different PC I came across some strange elapsed time issues.

I have everything set up in this game to be updated based on time passed since the last game loop, as one should in most cases, but on the second PC everything was way off.

I found out the issue was to do with creating a TimeSpan using the FromTicks() method. I made a little test using the following code:

class Program
{
    static void Main(string[] args)
    {
        System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
        sw.Start();
        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
        sw.Stop();
        TimeSpan t = TimeSpan.FromTicks(sw.ElapsedTicks);
        Console.WriteLine(t.ToString());
        Console.WriteLine(sw.Elapsed.ToString());
        Console.ReadKey();
    }
}

On my main PC, I ran this program and got the following:

    00:00:00.3528353
    00:00:00.9856987

Something I didn't expect at all. I thought the second result was quite inaccurate, but the first was well off.

Then I ran the same program on the other PC and got this:

    00:03:20.6866734
    00:00:00.998287

I was quite astounded.

My question here is not how I can fix this issue, I have already decided to use the second method because it’s accurate enough... rather, I ask for enlightenment.

How can this be so? Why is the first result so inaccurate? Why does this vary hugely on a different machine?

I checked on msdn in case I was using the method wrong, but the examples there show that my results should be impossible...

Note:
I think the CMOS battery is dying/dead, is that a factor?

Malloy answered 18/10, 2011 at 15:10 Comment(5)
The TimeSpan and Stopwatch use different "Tick" sizes. Thus you can't load a TimeSpan with the Ticks from a Stopwatch directly.Showily
See this link on why Stopwatch.Elapsed.Ticks and Stopwatch.ElapsedTicks are different. - #5794291Showily
@James is correct, just use ElapsedMilliseconds instead to avoid this confusion..Nyaya
Please tell me you ran it more then twice? Was the computer doing something else for 3 minutes or something?Scape
Of course, I ran it several times on both, with only slightly varying results. Always 3 minutes 20 though.Malloy
M
6

Summary: The Frequency of the stopwatch can be different on different hardware which means the ticks (whose interval is based on frequency) are of a different size (and of a different size to the tick in the timespan and datetime objects).

In short, use the Elapsed property directly instead:

    TimeSpan t = sw.Elapsed;

...or use the Ticks property of Elapsed, if you need to perform calculations:

    TimeSpan t = TimeSpan.FromTicks(2*sw.Elapsed.Ticks);

Long version with references:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsedticks.aspx is the msdn page for your elapsed ticks. Of note is:

This property represents the number of elapsed ticks in the underlying timer mechanism. A tick is the smallest unit of time that the Stopwatch timer can measure. Use the Frequency field to convert the ElapsedTicks value into a number of seconds.

And from the page on the Frequency field:

The timer frequency indicates the timer precision and resolution. For example, a timer frequency of 2 million ticks per second equals a timer resolution of 500 nanoseconds per tick. In other words, because one second equals 1 billion nanoseconds, a timer frequency of 2 million ticks per second is equivalent to 2 million ticks per 1 billion nanoseconds, which can be further simplified to 1 tick per 500 nanoseconds.

The Frequency value depends on the resolution of the underlying timing mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Frequency value reflects the frequency of that counter. Otherwise, the Frequency value is based on the system timer frequency.

Because the Stopwatch frequency depends on the installed hardware and operating system, the Frequency value remains constant while the system is running.

So essentially the frequency of the stopwatch can be different on different hardware which means the ticks are of a different size (and of a different size to the tick in the timespan and datetime objects).

Interestingly you are already using the Elapsed property StopWatch which gives you a timespan. sw.Elapsed is a TimeSpan which is probably what you are after when you are trying to get the TimeSpan object. If you want to use ticks you can use the Ticks property of this TimeSpan instead.

Alternatively you can use ElapsedMilliseconds which returns a long.

Mansoor answered 18/10, 2011 at 15:17 Comment(1)
"Interestingly you are already using the stopwatch Property that gives you a timespan" - Yeah, I originally used this in a base class for a game and it just worked well enough until now. I have no idea what I was thinking. Thanks very much for your answer though!Malloy
S
6

Timespan's Ticks and Stopwatch's Ticks are different. To reconcile, use the Stopwatch Elapsed property instead, which converts correctly to a TimeSpan based on the ticks of the machine.

    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); 
    sw.Start();         
    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));         
    sw.Stop();         

    // don't load a TimeSpan with ElapsedTicks, these have diff frequency,
    // call Elapsed instead to get TimeSpan.
    TimeSpan t = sw.Elapsed;         
    Console.WriteLine(t.ToString());         
    Console.ReadKey(); 

Or, you can of course divide by the frequency, etc, yourself, but it's a lot more work. The main takeaway is you should not consider TimeSpan's Ticks the same as Stopwatch's Ticks.

Related Thread: What are timer ticks, the unit used by Stopwatch.ElapsedTicks

Showily answered 18/10, 2011 at 15:17 Comment(0)
M
6

Summary: The Frequency of the stopwatch can be different on different hardware which means the ticks (whose interval is based on frequency) are of a different size (and of a different size to the tick in the timespan and datetime objects).

In short, use the Elapsed property directly instead:

    TimeSpan t = sw.Elapsed;

...or use the Ticks property of Elapsed, if you need to perform calculations:

    TimeSpan t = TimeSpan.FromTicks(2*sw.Elapsed.Ticks);

Long version with references:

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsedticks.aspx is the msdn page for your elapsed ticks. Of note is:

This property represents the number of elapsed ticks in the underlying timer mechanism. A tick is the smallest unit of time that the Stopwatch timer can measure. Use the Frequency field to convert the ElapsedTicks value into a number of seconds.

And from the page on the Frequency field:

The timer frequency indicates the timer precision and resolution. For example, a timer frequency of 2 million ticks per second equals a timer resolution of 500 nanoseconds per tick. In other words, because one second equals 1 billion nanoseconds, a timer frequency of 2 million ticks per second is equivalent to 2 million ticks per 1 billion nanoseconds, which can be further simplified to 1 tick per 500 nanoseconds.

The Frequency value depends on the resolution of the underlying timing mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Frequency value reflects the frequency of that counter. Otherwise, the Frequency value is based on the system timer frequency.

Because the Stopwatch frequency depends on the installed hardware and operating system, the Frequency value remains constant while the system is running.

So essentially the frequency of the stopwatch can be different on different hardware which means the ticks are of a different size (and of a different size to the tick in the timespan and datetime objects).

Interestingly you are already using the Elapsed property StopWatch which gives you a timespan. sw.Elapsed is a TimeSpan which is probably what you are after when you are trying to get the TimeSpan object. If you want to use ticks you can use the Ticks property of this TimeSpan instead.

Alternatively you can use ElapsedMilliseconds which returns a long.

Mansoor answered 18/10, 2011 at 15:17 Comment(1)
"Interestingly you are already using the stopwatch Property that gives you a timespan" - Yeah, I originally used this in a base class for a game and it just worked well enough until now. I have no idea what I was thinking. Thanks very much for your answer though!Malloy
P
1

Just for you information... I tested it and this is how to do it if you absolutely want to use "FromTicks". But If I were you, I would follow James Michael Hare advise.

    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
    sw.Start();
    System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));
    sw.Stop();

    // don't load a TimeSpan with ElapsedTicks, these have diff frequency,
    // call Elapsed instead to get TimeSpan.
    TimeSpan t1 = sw.Elapsed;
    Debug.Print("Millisecs: " + t1.TotalMilliseconds);


    // TimeSpan.Elapsed Code
    //if (!SafeNativeMethods.QueryPerformanceFrequency(out Stopwatch.Frequency))
    //{
    //  Stopwatch.IsHighResolution = false;
    //  Stopwatch.Frequency = 10000000L;
    //  Stopwatch.tickFrequency = 1.0;
    //}
    //else
    //{
    //  Stopwatch.IsHighResolution = true;
    //  Stopwatch.tickFrequency = 10000000.0;
    //  Stopwatch.tickFrequency /= (double)Stopwatch.Frequency;
    //}

    //public TimeSpan Elapsed
    //{
    //  [__DynamicallyInvokable]
    //  get
    //  {
    //      return new TimeSpan(this.GetElapsedDateTimeTicks());
    //  }
    //}

    //private long GetElapsedDateTimeTicks()
    //{
    //  long rawElapsedTicks = this.GetRawElapsedTicks();
    //  if (Stopwatch.IsHighResolution)
    //      return (long)((double)rawElapsedTicks * Stopwatch.tickFrequency);
    //  return rawElapsedTicks;
    //}

    TimeSpan t2;
    if (Stopwatch.IsHighResolution)
    {
        t2 = TimeSpan.FromTicks((long)((double)sw.ElapsedTicks * ((double)10000000.0 / (double)Stopwatch.Frequency)));
    }
    else
    {
        t2 = TimeSpan.FromTicks(sw.ElapsedTicks);
    }
    Debug.Assert(t1.TotalMilliseconds == t2.TotalMilliseconds); // true, 

    return;
Popovich answered 19/11, 2016 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.