简体   繁体   English

程序执行速度测试

[英]Program Execution Speed Testing

I have a C# app in which I am testing 3 ways to do something with XML. 我有一个C#应用程序,其中正在测试3种使用XML进行操作的方法。

I read a string of XML get a value out and then add it to a custom collection. 我读了一个XML字符串,得到一个值,然后将其添加到自定义集合中。

I am testing XMLDocument, XDocument and String manipulation to find the node value, in that order recording a start and end time for each. 我正在测试XMLDocument,XDocument和String操作以查找节点值,以该顺序记录每个节点的开始和结束时间。

However if I say mix the order up I get different speeds reported. 但是,如果我说混合使用顺序,则会收到不同的速度报告。 Also when I press the button again to perform the test again the one that was slowest initially sometimes takes the least on subsequent tests. 同样,当我再次按下按钮以再次执行测试时,最初最慢的测试有时在随后的测试中花费最少。

I'm sure its all related to JIT and Garbage collection but what is the truest way to determine what runs the fastest. 我敢肯定,这一切都与JIT和垃圾回收有关,但是确定运行速度最快的最正确的方法是什么。

I have downloaded EQATEC profiler but I'm lost on what to do with it. 我已经下载了EQATEC探查器,但是却迷失了如何使用它。

Can someone advise me the best way to determine what methods run the quickest. 有人可以建议我确定哪种方法运行最快的最佳方法。

Thanks 谢谢

另一个可能值得尝试的探查器是Slimtune

使用StopWatch测量时间时,使用一组样本输入是否足以满足每种方法的要求?

Jon Skeet has written a pretty good bechmarking 'framework' for measuring simple operations like this that I think would be pretty well suited to checking each of those methods, which you could find here: http://www.yoda.arachsys.com/csharp/benchmark.html (note, this is just timing the operations, not profiling the heap usage or anything like that). 乔恩斯基特写了一个相当不错的bechmarking“框架”,用于测量像这样简单的操作,我认为将是非常适合于检查每个这些方法,你可以在这里找到: http://www.yoda.arachsys.com/ csharp / benchmark.html (注意,这只是对操作进行计时,而不是对堆使用情况进行分析或类似的操作)。

An important point to keep in mind (Jon notes it on that site above, and Eric Lippert is fond of mentioning it whenever the topic comes up) is that when you run code multiple times, it only gets JIT'd the first time, so if you want to measure the true differences for each method, measure at least twice and compare the second runs - I would imagine this is the result of your shifting orders. 需要牢记的重要一点(乔恩在上面的网站上提到过,埃里克·利珀特喜欢在主题出现时就提到它)是,当您多次运行代码时,它只会在第一次得到JIT,因此如果您想测量每种方法的真正差异,请至少测量两次并比较第二次运行-我想这是您转移订单的结果。

Try using a profiling tool like ANTS from Red-Gate or dotTrace from JetBrains. 尝试使用分析工具,例如Red-Gate的ANTS或JetBrains的dotTrace The profilers will show you what percentage of time is spent in each method allowing you to identify which is the faster executed method. 探查器将向您显示每种方法所花费的时间百分比,从而使您能够确定执行速度更快的方法。

Learn to use the tools you have: 学习使用您拥有的工具:

http://www.eqatec.com/tools/profiler/guide http://www.eqatec.com/tools/profiler/guide

If you're getting inconsistent results, do the scientific thing and make control cases: 如果结果不一致,请做科学的事情并进行控制:

  • Either test multiple times in a loop so the first case is averaged out among the rest of the runs 可以在一个循环中多次测试,以便在其余运行中将第一种情况平均化
  • or just test the first case (restarting the app each time). 或仅测试第一种情况(每次重新启动应用程序)。

You can use Timing class from "Data structures and algorithms using c#" by Michael McMillian. 您可以使用Michael McMillian的“使用c#的数据结构和算法”中的Timing类。

Roughly it'll be as follows: 大致如下:

TimeSpan startingTime, duration;
GC.Collect();
GC.WaitForPendingFinalizers();
startingTime = Process.GetCurrentProcess().Threads[0].UserProcessorTime;
DoLengthyWork();
duration = Process.GetCurrentProcess.Threads(0).UserProcessorTime.Subtract(startingTime);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM