How can I determine the amount of time a set of operations in C# require to execute?
是否有一个高精度计时器,我可以用来衡量执行时一系列操作/语句所花费的时间? 返回自纪元以来的秒数的计时器将不足,因为分辨率以秒为单位。
我认为一般的实施将是:
谢谢,
斯科特
秒表类适用于此。 请注意,它测量挂钟时间,而不是代码执行时间。
1 2 3 4 5 | Stopwatch sw= new Stopwatch(); sw.Start(); DoTimeConsumingOperation(); sw.Stop(); Console.WriteLine(sw.Elapsed); // in milliseconds |
The Stopwatch measures elapsed time by
counting timer ticks in the underlying
timer mechanism. If the installed
hardware and operating system support
a high-resolution performance counter,
then the Stopwatch class uses that
counter to measure elapsed time.
Otherwise, the Stopwatch class uses
the system timer to measure elapsed
time. Use the Frequency and
IsHighResolution fields to determine
the precision and resolution of the
Stopwatch timing implementation.
您可以使用