Dav*_*rab 466 .net c# performance datetime timer
我需要找到一个瓶颈,并且需要准确地测量时间.
以下代码段是衡量性能的最佳方法吗?
DateTime startTime = DateTime.Now;
// Some execution process
DateTime endTime = DateTime.Now;
TimeSpan totalTimeTaken = endTime.Subtract(startTime);
Run Code Online (Sandbox Code Playgroud)
Mar*_*son 642
不,这不对.使用秒表(in System.Diagnostics)
Stopwatch sw = Stopwatch.StartNew();
PerformWork();
sw.Stop();
Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);
Run Code Online (Sandbox Code Playgroud)
秒表会自动检查是否存在高精度计时器.
值得一提的是,由于必须使用时区,DST等进行的工作,DateTime.Now通常要慢得多.DateTime.UtcNow
DateTime.UtcNow通常具有15毫秒的分辨率.请参阅John Chapman关于DateTime.Now精确度的博客文章,以获得精彩的摘要.
有趣的琐事:DateTime.UtcNow如果您的硬件不支持高频计数器,秒表会重新开始.您可以通过查看静态字段Stopwatch.IsHighResolution来检查秒表是否使用硬件来实现高精度.
mmc*_*ole 91
如果你想要快速和肮脏的东西,我会建议使用秒表,以获得更高的精确度.
Stopwatch sw = new Stopwatch();
sw.Start();
// Do Work
sw.Stop();
Console.WriteLine("Elapsed time: {0}", sw.Elapsed.TotalMilliseconds);
Run Code Online (Sandbox Code Playgroud)
或者,如果您需要更复杂的东西,您应该考虑使用第三方分析器,例如ANTS.
Val*_*zub 55
这篇文章说,首先你需要比较三种选择Stopwatch,DateTime.NowAND DateTime.UtcNow.
它还显示在某些情况下(当性能计数器不存在时)秒表正在使用DateTime.UtcNow +一些额外的处理.因为很明显,在这种情况下,DateTime.UtcNow是最好的选择(因为其他使用它+一些处理)
然而,事实证明,计数器几乎总是存在 - 请参阅有关高分辨率性能计数器及其与.NET秒表相关的解释?.
这是一个性能图表.请注意UtcNow与替代品相比性能成本如何低:

X轴是样本数据大小,Y轴是示例的相对时间.
有一点Stopwatch是更好的是它提供更高分辨率的时间测量.另一个是它更具OO性质.但是,创建一个OO包装器UtcNow并不难.
Ant*_*ean 18
将基准测试代码推送到实用程序类/方法中非常有用.本StopWatch类并不需要是Disposed或Stopped出错.所以,最简单的代码时有些动作是
public partial class With
{
public static long Benchmark(Action action)
{
var stopwatch = Stopwatch.StartNew();
action();
stopwatch.Stop();
return stopwatch.ElapsedMilliseconds;
}
}
Run Code Online (Sandbox Code Playgroud)
样本调用代码
public void Execute(Action action)
{
var time = With.Benchmark(action);
log.DebugFormat(“Did action in {0} ms.”, time);
}
Run Code Online (Sandbox Code Playgroud)
这是扩展方法版本
public static class Extensions
{
public static long Benchmark(this Action action)
{
return With.Benchmark(action);
}
}
Run Code Online (Sandbox Code Playgroud)
并调用代码示例
public void Execute(Action action)
{
var time = action.Benchmark()
log.DebugFormat(“Did action in {0} ms.”, time);
}
Run Code Online (Sandbox Code Playgroud)
rp.*_*rp. 13
使用System.Diagnostics.Stopwatch类.
Stopwatch sw = new Stopwatch();
sw.Start();
// Do some code.
sw.Stop();
// sw.ElapsedMilliseconds = the time your "do some code" took.
Run Code Online (Sandbox Code Playgroud)
And*_*nea 11
Ditto秒表,它的方式更好.
关于绩效衡量,您还应该检查您的"//一些执行流程"是否是一个非常短的过程.
还要记住,"// Some Execution Process"的第一次运行可能比后续运行慢.
我通常通过在循环中运行1000次或1000000次来测试方法,并且我获得比运行一次更准确的数据.
这些都是衡量时间的好方法,但这只是找到瓶颈的一种非常间接的方式.
在线程中找到瓶颈的最直接方法是让它运行,当它正在做任何让你等待的事情时,用暂停或中断键停止它.这样做几次.如果您的瓶颈占用X%的时间,则X%是您在每个快照的行为中捕获它的概率.
@ 肖恩钱伯斯
仅供参考,.NET Timer类不用于诊断,它以预设的间隔生成事件,如下所示(来自MSDN):
System.Timers.Timer aTimer;
public static void Main()
{
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
Run Code Online (Sandbox Code Playgroud)
所以这真的无法帮助你知道事情花了多长时间,只是已经过了一段时间.
计时器也作为System.Windows.Forms中的控件公开...你可以在VS05/VS08的设计器工具箱中找到它
Visual Studio Team System具有一些可能有助于解决此问题的功能.基本上,您可以编写单元测试并将它们混合在不同的场景中,以作为压力或负载测试的一部分运行.这可能有助于确定最能影响应用程序性能的代码区域.
Microsoft的模式和实践组在Visual Studio Team System性能测试指南中有一些指导.
小智 6
这是正确的方法:
using System;
using System.Diagnostics;
class Program
{
public static void Main()
{
Stopwatch stopWatch = Stopwatch.StartNew();
// some other code
stopWatch.Stop();
// this not correct to get full timer resolution
Console.WriteLine("{0} ms", stopWatch.ElapsedMilliseconds);
// Correct way to get accurate high precision timing
Console.WriteLine("{0} ms", stopWatch.Elapsed.TotalMilliseconds);
}
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请使用Stopwatch而不是DataTime来获取准确的性能计数器.
我在我的程序中使用的方式是使用 StopWatch 类,如下所示。
Stopwatch sw = new Stopwatch();
sw.Start();
// Critical lines of code
long elapsedMs = sw.Elapsed.TotalMilliseconds;
Run Code Online (Sandbox Code Playgroud)
小智 5
这还不够专业:
Stopwatch sw = Stopwatch.StartNew();
PerformWork();
sw.Stop();
Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds);
Run Code Online (Sandbox Code Playgroud)
一个更可靠的版本是:
PerformWork();
int repeat = 1000;
Stopwatch sw = Stopwatch.StartNew();
for (int i = 0; i < repeat; i++)
{
PerformWork();
}
sw.Stop();
Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds / repeat);
Run Code Online (Sandbox Code Playgroud)
在我的真实代码中,我将添加GC.Collect调用以将托管堆更改为已知状态,并添加Sleep调用,以便可以在ETW配置文件中轻松分隔不同的代码间隔。
| 归档时间: |
|
| 查看次数: |
63149 次 |
| 最近记录: |