Difference between ElapsedTicks, ElapsedMilliseconds, Elapsed.Milliseconds and Elapsed.TotalMilliseconds? (C#)
Asked Answered
M

4

48

I'm totally confused between these 4. What is the difference between ElapsedMilliseconds (long), ElapsedTicks (long), Elapsed.TotalMilliseconds (double) and Elapsed.Milliseconds (int)?

I have a function

    {
        Stopwatch sw = new Stopwatch();

        sw.Start();
        MyTimeConsumingAction();
        sw.Stop();

        sw.//what?
    }

How do I get the correct time consumed by my long running process from elapsed property of Stopwatch object in milliseconds?

Edit: I tried msdn documentation but it isn't anything detailed there..

Morocco answered 17/1, 2012 at 12:15 Comment(1)
Also see this #5794291Morocco
F
64

Elapsed.TotalMilliseconds (double) returns the total number of whole and fractional milliseconds elapsed since inception

e.g. a stopwatch stopped at 1.23456 seconds would return 1234.56 in this property. See TimeSpan.TotalMilliseconds on MSDN

Elapsed.Milliseconds (int) returns the number of whole milliseconds in the current second

e.g. a stopwatch at 1.234 seconds would return 234 in this property. See TimeSpan.Milliseconds

ElapsedTicks (long) returns the ticks since start of the stopwatch.

In the context of the original question, pertaining to the Stopwatch class, ElapsedTicks is the number of ticks elapsed. Ticks occur at the rate of Stopwatch.Frequency, so, to compute seconds elapsed, calculate: numSeconds = stopwatch.ElapsedTicks / Stopwatch.Frequency.

The old answer defined ticks as the number of 100 nanosecond periods, which is correct in the context of the DateTime class, but not correct in the context of the Stopwatch class. See Stopwatch.ElapsedTicks on MSDN.

ElapsedMilliseconds returns a rounded number to the nearest full millisecond, so this might lack precision Elapsed.TotalMilliseconds property can give.

Elapsed.TotalMilliseconds is a double that can return execution times to the partial millisecond while ElapsedMilliseconds is Int64. e.g. a stopwatch at 0.0007 milliseconds would return 0, or 1234.56 milliseconds would return 1234 in this property. So for precision always use Elapsed.TotalMilliseconds.

See Stopwatch.ElapsedMilliseconds on MSDN for clarification.

Flinger answered 17/1, 2012 at 12:20 Comment(2)
With sw as in the question, do note that: sw.ElapsedTicks gives the number of ticks in the hardware dependent unit Stopwatch.Frequency, just like you say. However, sw.Elapsed.Ticks (note the extra dot!!) gives the calculated number of ticks in the 100 nanoseconds unit implied by the constant TimeSpan.TicksPerSecond whose value is constant 10,000,000.Bundle
Thank you SO MUCH for emphasing this subtle difference. I was using ElapsedMilliseconds in my own benchmark and was wondering why my results were inconsistents.Yokel
E
9

Reflecting the Stopwatch class reveals that ElapsedMilliseconds is Elapsed ticks converted (and rounded) to milliseconds:

public TimeSpan Elapsed
{
  get
  {
    return new TimeSpan(this.GetElapsedDateTimeTicks());
  }
}

public long ElapsedMilliseconds
{
  get
  {
    return this.GetElapsedDateTimeTicks() / 10000L;
  }
}
Erstwhile answered 17/1, 2012 at 12:29 Comment(2)
This is the correct reference to the ElapsedMilliseconds, as asked by the OP. The accepted answer is not using the Stopwatch classes 'ElapsedMilliseconds', this can be confusing as the link had the incorrect text description - now fixedNaples
Beware, this is NOT right for the current version of .net. You need to consider Stopwatch.Frequency when you convert the tick to ms, not simply divide it by 10000.Lactescent
S
5

in a short explanation from msdn:

ElapsedMilliseconds

This property represents elapsed time rounded down to the nearest whole millisecond value. For higher precision measurements, use the Elapsed or ElapsedTicks properties.

ElapsedTicks

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.

Elapsed

Use the Elapsed property to retrieve the elapsed time value using TimeSpan methods and properties. For example, you can format the returned TimeSpan instance into a text representation, or pass it to another class that requires a TimeSpan parameter.

Sher answered 17/1, 2012 at 12:24 Comment(0)
B
1

Elapsed is TimeSpan. If you want to display time, then just Elapsed.ToString() should do that

Byelaw answered 17/1, 2012 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.