ElapsedTicks、ElapsedMilliseconds、Elapsed.Milliseconds 和 Elapsed.TotalMilliseconds 之间的区别? (C#)
我对这4个完全感到困惑。ElapsedMilliseconds (long)、ElapsedTicks (long)、Elapsed.TotalMilliseconds (double) 和 Elapsed.Milliseconds (int) 之间有什么区别?
我有一个函数
{
Stopwatch sw = new Stopwatch();
sw.Start();
MyTimeConsumingAction();
sw.Stop();
sw.//what?
}
如何从 Stopwatch 对象的 elapsed 属性中获取长时间运行的进程消耗的正确时间(以毫秒为单位)?
编辑:我尝试了 msdn 文档,但那里没有任何详细信息。
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
例如停在 1.23456 秒的秒表将在此属性中返回 1234.56。请参阅 MSDN 上的 TimeSpan.TotalMilliseconds
例如1.234 秒的秒表将在此属性中返回 234。请参阅 TimeSpan.Milliseconds
在原始问题的上下文中,与 Stopwatch 类相关,
ElapsedTicks
是经过的刻度数。刻度以Stopwatch.Frequency
的速率发生,因此,要计算经过的秒数,请计算:numSeconds = stopwatch.ElapsedTicks / Stopwatch.Frequency
。旧答案将刻度定义为 100 纳秒周期的数量,这在 DateTime 类的上下文中是正确的,但在 Stopwatch 类的上下文中是不正确的。请参阅 MSDN 上的 Stopwatch.ElapsedTicks。
Elapsed.TotalMilliseconds
是一个double
,可以将执行时间返回到部分毫秒,而ElapsedMilliseconds
是Int64
。例如,在此属性中,0.0007 毫秒的秒表将返回 0,或者 1234.56 毫秒将返回 1234。因此,为了保证精度,请始终使用Elapsed.TotalMilliseconds
。有关说明,请参阅 MSDN 上的 Stopwatch.ElapsedMilliseconds。
e.g. a stopwatch stopped at 1.23456 seconds would return 1234.56 in this property. See TimeSpan.TotalMilliseconds on MSDN
e.g. a stopwatch at 1.234 seconds would return 234 in this property. See TimeSpan.Milliseconds
In the context of the original question, pertaining to the Stopwatch class,
ElapsedTicks
is the number of ticks elapsed. Ticks occur at the rate ofStopwatch.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.
Elapsed.TotalMilliseconds
is adouble
that can return execution times to the partial millisecond whileElapsedMilliseconds
isInt64
. 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 useElapsed.TotalMilliseconds
.See Stopwatch.ElapsedMilliseconds on MSDN for clarification.
反映 Stopwatch 类可以发现 ElapsedMilliseconds 是将经过的刻度转换(并四舍五入)为毫秒:
Reflecting the Stopwatch class reveals that ElapsedMilliseconds is Elapsed ticks converted (and rounded) to milliseconds:
msdn 的简短解释:
此属性表示已过去时间向下舍入到最接近的整毫秒值。对于更高精度的测量,请使用 Elapsed 或 ElapsedTicks 属性。
该属性表示底层计时器机制中经过的滴答数。滴答声是秒表计时器可以测量的最小时间单位。使用Frequency 字段将ElapsedTicks 值转换为秒数。
使用 Elapsed 属性可通过 TimeSpan 方法和属性检索经过的时间值。例如,您可以将返回的 TimeSpan 实例格式化为文本表示形式,或将其传递给需要 TimeSpan 参数的另一个类。
in a short explanation from msdn:
This property represents elapsed time rounded down to the nearest whole millisecond value. For higher precision measurements, use the Elapsed or ElapsedTicks properties.
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.
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
。如果你想显示时间,那么只需Elapsed.ToString()
就可以了Elapsed
isTimeSpan
. If you want to display time, then justElapsed.ToString()
should do that