性能测试的精确时间测量 [英] Exact time measurement for performance testing
本文介绍了性能测试的精确时间测量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查看某项(例如方法调用)在代码中占用多长时间的最准确方法是什么?
What is the most exact way of seeing how long something, for example a method call, took in code?
我猜最简单和最快的方法是:
The easiest and quickest I would guess is this:
DateTime start = DateTime.Now;
{
// Do some work
}
TimeSpan timeItTook = DateTime.Now - start;
但这有多准确?有没有更好的方法?
But how exact is this? Are there better ways?
推荐答案
更好的方法是使用 Stopwatch 类:
A better way is to use the Stopwatch class:
using System.Diagnostics;
// ...
Stopwatch sw = new Stopwatch();
sw.Start();
// ...
sw.Stop();
Console.WriteLine("Elapsed={0}",sw.Elapsed);
这篇关于性能测试的精确时间测量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文