精确的时间测量性能测试 [英] Exact time measurement for performance testing

查看:165
本文介绍了精确的时间测量性能测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是看多长时间的东西,例如一个方法调用的最准确的方式,参加了code?

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?

推荐答案

一个更好的办法是使用秒表类:

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屋!

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