C# ElapsedTicks、ElapsedMilliseconds、Elapsed.Milliseconds 和 Elapsed.TotalMilliseconds 之间的区别?(C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8894425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:00:42  来源:igfitidea点击:

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

c#performance.net-4.0system.diagnosticsstopwatch

提问by nawfal

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

我完全混淆了这 4 个。 ElapsedMilliseconds (long), ElapsedTicks (long), Elapsed.TotalMilliseconds (double) 和 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?

如何从秒表对象的 elapsed 属性中以毫秒为单位获得长时间运行的进程消耗的正确时间?

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

编辑:我尝试过 msdn 文档,但那里没有任何详细信息..

采纳答案by Dr. ABT

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

Elapsed.TotalMilliseconds (double) 返回自开始以来经过的整数和小数毫秒总数

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

例如,停在 1.23456 秒的秒表将在此属性中返回 1234.56。请参阅MSDN 上的TimeSpan.TotalMilliseconds

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

Elapsed.Milliseconds(int) 返回当前秒的整毫秒数

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

例如,1.234 秒的秒表将在此属性中返回 234。请参阅TimeSpan.Milliseconds

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

ElapsedTicks (long) 返回自秒表开始以来的刻度。

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.

在原始问题的上下文中,与秒表类有关,ElapsedTicks 是经过的滴答数。Ticks 以 Stopwatch.Frequency 的速率发生,因此,要计算经过的秒数,请计算: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.ElapsedTickson MSDN.

旧答案将刻度定义为 100 纳秒周期的数量,这在 DateTime 类的上下文中是正确的,但在 Stopwatch 类的上下文中不正确。请参阅 MSDN 上的Stopwatch.ElapsedTicks

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

ElapsedMilliseconds 返回一个舍入到最接近完整毫秒的数字,因此这可能缺乏 Elapsed.TotalMilliseconds 属性可以提供的精度。

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.

Elapsed.TotalMilliseconds 是一个双精度值,可以将执行时间返回到部分毫秒,而 ElapsedMilliseconds 是 Int64。例如,在 0.0007 毫秒的秒表将返回 0 ,或者 1234.56 毫秒将在此属性中返回 1234。所以为了精确,总是使用 Elapsed.TotalMilliseconds。

See Stopwatch.ElapsedMilliseconds on MSDNfor clarification.

有关说明,请参阅MSDN 上的 Stopwatch.ElapsedMilliseconds

Best regards,

此致,

回答by Renatas M.

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

ElapsedTimeSpan。如果你想显示时间,那么就Elapsed.ToString()应该这样做

回答by Gustavo F

in a short explanation from msdn:

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.

此属性表示向下舍入到最接近的整数毫秒值的经过时间。对于更高精度的测量,请使用 Elapsed 或 ElapsedTicks 属性。

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.

此属性表示底层计时器机制中经过的滴答数。刻度是秒表计时器可以测量的最小时间单位。使用频率字段将 ElapsedTicks 值转换为秒数。

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.

使用 Elapsed 属性通过 TimeSpan 方法和属性检索经过的时间值。例如,您可以将返回的 TimeSpan 实例格式化为文本表示形式,或将其传递给另一个需要 TimeSpan 参数的类。

回答by granaker

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

反映 Stopwatch 类显示 ElapsedMilliseconds 是 Elapsed ticks 转换(并四舍五入)到毫秒:

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

public long ElapsedMilliseconds
{
  get
  {
    return this.GetElapsedDateTimeTicks() / 10000L;
  }
}