Ben*_*iel 10 .net c# performance stopwatch
例如
foo() //Some operation bound by an external resource. db,I/O, whatever.
Run Code Online (Sandbox Code Playgroud)
与
var watch = new Stopwatch();
watch.Start();
foo()
var time = watch.ElapsedMilliseconds
watch.Stop();
Run Code Online (Sandbox Code Playgroud)
我相信秒表是建立在QueryPerformanceCounter之上的,因此每次调用都会导致内核转换.如果foo()非常简短,那么QPC开销会使它相形见绌.
如果你使用秒表来测量短任务,你应该多次运行foo()(比如数千),并在整个批处理中使用秒表.将总时间除以运行次数以获得任务的平均时间.