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

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

问题描述

我这4之间完全糊涂ElapsedMilliseconds(长),ElapsedTicks(长),Elapsed.TotalMilliseconds(双)和Elapsed.Milliseconds(INT)之间的区别是什么?

我有一个函数

  {
        秒表SW =新的秒表();

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

        SW //什么?
    }
 

我如何从秒表对象经过物业以毫秒为单位消耗我的长时间运行的过程中,正确的时间?

编辑:我试过MSDN文档,但没有什么具体有..

解决方案
  

Elapsed.TotalMilliseconds(双)返回整个的总数,自成立以来经过的毫秒小数

例如。秒表停在1.23456秒将在此属性返回1234.56。请参阅<一href="http://msdn.microsoft.com/en-us/library/system.timespan.totalmilliseconds.aspx">TimeSpan.TotalMilliseconds MSDN上

  

Elapsed.Milliseconds(INT)返回整个毫秒当前第二号

例如。秒表在1.234秒将在此属性返回234。请参阅<一href="http://msdn.microsoft.com/en-us/library/system.timespan.milliseconds.aspx">TimeSpan.ElapsedMilliseconds

  

ElapsedTicks(长)返回蜱,因为秒表的开始。

在原来问题的背景下,有关秒表类,ElapsedTicks是蜱经过数。蜱发生在Stopwatch.Frequency的速度,因此,计算经过秒,计算:numSeconds = stopwatch.ElapsedTicks / Stopwatch.Frequency。

定义的旧答案蜱如100纳秒周期数,这是在DateTime类的上下文中正确的,但在秒表类的上下文不正确的。请参阅<一href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsedticks.aspx">Stopwatch.ElapsedTicks MSDN上。

  

ElapsedMilliseconds返回一个圆形的数量到最近的毫秒,因此这可能缺乏precision Elapsed.TotalMilliseconds属性可以给。

Elapsed.TotalMilliseconds是一个双能返回执行时间,以局部毫秒,而ElapsedMilliseconds是Int64的。例如秒表在0.0007毫秒将返回0,或1234.56毫秒将在此属性返回1234。因此,对于precision总是用Elapsed.TotalMilliseconds。

请参阅<一href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.elapsedmilliseconds.aspx">Stopwatch.ElapsedMilliseconds 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..

解决方案

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.ElapsedMilliseconds

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.

Best regards,

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

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