在C#中使用时间戳来测试方法长度

Ice*_*ker 1 .net c# timestamp

我想测试一个方法运行需要多长时间,我想知道时间跨度是否是最佳方式?我不想记录方法的开始和结束,因为我听说由于创建和更新现有日志文件所花费的时间,日志记录将无法准确读取时间.如果时间跨度不是最好的方法,那么什么是一个好方法呢?

Sys*_*own 6

最好使用专为此类设计的秒表类.

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
DoSomething();
stopWatch.Stop();
// Get the elapsed time as a TimeSpan value.
TimeSpan ts = stopWatch.Elapsed;
Run Code Online (Sandbox Code Playgroud)