在 Windows 中对程序进行基准测试的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/145103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best way to benchmark programs in Windows?
提问by Ben Collins
I need to do some performance benchmarks on .NET programs (C#) in Windows, but I haven't done benchmarking much in the Windows world. I've looked into using the Windows 2000/XP Performance monitor with custom counters for this, but I don't think this is quite what I want.
我需要对 Windows 中的 .NET 程序 (C#) 进行一些性能基准测试,但我在 Windows 世界中并没有做过很多基准测试。为此,我研究了使用带有自定义计数器的 Windows 2000/XP 性能监视器,但我认为这不是我想要的。
Are there any good system facilities for this in Windows XP, or do I need to just use System.Diagnostics.Stopwatch [edit] and write text logs for manual interpretation, or is there something else?
在 Windows XP 中是否有任何好的系统设施,或者我是否只需要使用 System.Diagnostics.Stopwatch [edit] 并编写文本日志以进行手动解释,或者还有其他东西吗?
Edit: is there anything beyond System.Diagnostics.Stopwatch
?
编辑:还有什么超出System.Diagnostics.Stopwatch
?
采纳答案by Wes Haggard
For micro-benchmarking I really like MeasureIt (can be downloaded from http://msdn.microsoft.com/en-us/magazine/cc500596.aspx). It is a test project written by Vance Morrison a Performance Architect on the CLR. It currently has a good set of benchmarks for a number of .Net/CLR core methods. The best part of it is that it is trivial to tweak and add new benchmarks for whatever you would like to test. Simply run "MeasureIt /edit" and it will launch VS with the project for itself so that you can view how those benchmarks are written and add new ones in a similar fashion if you like.
对于微基准测试,我真的很喜欢 MeasureIt(可以从http://msdn.microsoft.com/en-us/magazine/cc500596.aspx下载)。它是由性能架构师 Vance Morrison 在 CLR 上编写的测试项目。它目前为许多 .Net/CLR 核心方法提供了一套很好的基准测试。最好的部分是,为您想要测试的任何内容调整和添加新的基准是微不足道的。只需运行“MeasureIt /edit”,它就会为自己的项目启动 VS,这样您就可以查看这些基准测试的编写方式,并根据需要以类似的方式添加新的基准测试。
As already been stated StopWatch is probably the easiest way to do this and MeasureIt uses StopWatch underneath for its timings but it also does some other things like running a block of code X times and then providing you stats for the runs and what not.
正如已经说过的,StopWatch 可能是最简单的方法,MeasureIt 使用下面的 StopWatch 来计时,但它也做一些其他的事情,比如运行代码块 X 次,然后为您提供运行的统计数据,什么不是。
回答by Mitch Wheat
using System.Diagnostics;
....
Stopwatch sw = new Stopwatch();
sw.Start();
// Code you want to time...
// Note: for averaged accuracy (without other OS effects),
// run timed code multiple times in a loop
// and then divide by the number of runs.
sw.Stop();
Console.WriteLine("Took " + sw.ElapsedTicks + " Ticks");
回答by Justin R.
If you want just some quick numbers, you can use Powershell to time overall execution. Use the measure-command cmdlet. It's roughly equivalent to "time" in Unix.
如果您只需要一些快速数字,您可以使用 Powershell 来计算整体执行时间。使用测量命令 cmdlet。它大致相当于 Unix 中的“时间”。
> measure-command { your.exe arg1 }
Days : 0
Hours : 0
Minutes : 0
Seconds : 4
Milliseconds : 996
Ticks : 49963029
TotalDays : 5.78275798611111E-05
TotalHours : 0.00138786191666667
TotalMinutes : 0.083271715
TotalSeconds : 4.9963029
TotalMilliseconds : 4996.3029
回答by Franci Penov
There are quite a few profilers out there. Here are some of the ones I know of:
那里有很多分析器。以下是我所知道的一些:
If you go on with using System.Diagnostics.Stopwatch, you will be able to instrument and measure only particular points of your code, where you explicitly put the Start/Stop around. This is good enough for measuring specific pieces, like a tight loop or things like that, but it's not going to give you a complete picture of where your program spends most of its time.
如果继续使用 System.Diagnostics.Stopwatch,您将能够仅检测和测量代码的特定点,在这些点明确放置开始/停止。这对于测量特定的部分已经足够了,比如一个紧密的循环或类似的东西,但它不会让你完整地了解你的程序大部分时间都花在哪里。
回答by user23126
回答by Pablo Yabo
If your project is quite big and there are lots of modules doing big number of calls you can your: http://www.moduleanalyzer.com/
如果您的项目相当大并且有很多模块进行大量调用,您可以:http: //www.moduleanalyzer.com/