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

发布于 2024-12-27 19:09:43 字数 425 浏览 1 评论 0原文

我对这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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两相知 2025-01-03 19:09:43

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

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

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

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

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

在原始问题的上下文中,与 Stopwatch 类相关,ElapsedTicks 是经过的刻度数。刻度以 Stopwatch.Frequency 的速率发生,因此,要计算经过的秒数,请计算:numSeconds = stopwatch.ElapsedTicks / Stopwatch.Frequency

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

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

Elapsed.TotalMilliseconds 是一个 double,可以将执行时间返回到部分毫秒,而 ElapsedMillisecondsInt64。例如,在此属性中,0.0007 毫秒的秒表将返回 0,或者 1234.56 毫秒将返回 1234。因此,为了保证精度,请始终使用 Elapsed.TotalMilliseconds

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

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.

叹梦 2025-01-03 19:09:43

反映 Stopwatch 类可以发现 ElapsedMilliseconds 是将经过的刻度转换(并四舍五入)为毫秒:

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

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

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;
  }
}
琉璃繁缕 2025-01-03 19:09:43

msdn 的简短解释:

ElapsedMilliseconds

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

ElapsedTicks

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

Elapsed

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

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.

万水千山粽是情ミ 2025-01-03 19:09:43

ElapsedTimeSpan。如果你想显示时间,那么只需 Elapsed.ToString() 就可以了

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文