ElapsedTicks、ElapsedMilliseconds、Elapsed.Milliseconds 和 Elapsed.TotalMilliseconds 之间的区别?(C#) [英] Difference between ElapsedTicks, ElapsedMilliseconds, Elapsed.Milliseconds and Elapsed.TotalMilliseconds? (C#)

查看:46
本文介绍了ElapsedTicks、ElapsedMilliseconds、Elapsed.Milliseconds 和 Elapsed.TotalMilliseconds 之间的区别?(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对这 4 个完全困惑.ElapsedMilliseconds (long)、ElapsedTicks (long)、Elapsed.TotalMilliseconds (double) 和 Elapsed.Milliseconds (int) 之间有什么区别?

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

我有一个功能

    {
        Stopwatch sw = new Stopwatch();

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

        sw.//what?
    }

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

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

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

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

推荐答案

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

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

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

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

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

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

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

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

ElapsedTicks (long) 返回自秒表开始以来的滴答声.

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

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

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.

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

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 返回一个四舍五入的数字到最接近的完整毫秒,因此这可能缺乏 Elapsed.TotalMilliseconds 属性可以提供的精度.

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

Elapsed.TotalMilliseconds 是一个 double,可以将执行时间返回到部分毫秒,而 ElapsedMillisecondsInt64.例如在此属性中,0.0007 毫秒的秒表将返回 0,或 1234.56 毫秒将返回 1234.所以为了精确总是使用 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.

请参阅 MSDN 上的 Stopwatch.ElapsedMilliseconds 澄清.

See Stopwatch.ElapsedMilliseconds on MSDN for clarification.

这篇关于ElapsedTicks、ElapsedMilliseconds、Elapsed.Milliseconds 和 Elapsed.TotalMilliseconds 之间的区别?(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆