程序执行速度测试

发布于 2024-08-08 07:33:46 字数 362 浏览 19 评论 0 原文

我有一个 C# 应用程序,正在其中测试 3 种使用 XML 执行操作的方法。

我读取一个 XML 字符串并获取一个值,然后将其添加到自定义集合中。

我正在测试 XMLDocument、XDocument 和字符串操作以查找节点值,按顺序记录每个值的开始和结束时间。

但是,如果我说混合顺序,我会报告不同的速度。此外,当我再次按下按钮再次执行测试时,最初最慢的测试有时在后续测试中花费最少。

我确信这一切都与 JIT 和垃圾收集有关,但确定运行速度最快的最真实的方法是什么。

我已经下载了 EQATEC profiler,但我不知道如何使用它。

有人可以告诉我最好的方法来确定哪些方法运行最快。

谢谢

I have a C# app in which I am testing 3 ways to do something with XML.

I read a string of XML get a value out and then add it to a custom collection.

I am testing XMLDocument, XDocument and String manipulation to find the node value, in that order recording a start and end time for each.

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.

I have downloaded EQATEC profiler but I'm lost on what to do with it.

Can someone advise me the best way to determine what methods run the quickest.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

二智少女 2024-08-15 07:33:46

另一个可能值得尝试的分析器是 Slimtune

Another profiler that might be worth trying is Slimtune.

深陷 2024-08-15 07:33:46

使用 秒表

Isn't it sufficient to each method with a set of sample input while measuring the time with StopWatch?

云巢 2024-08-15 07:33:46

Jon Skeet 编写了一个非常好的基准测试“框架”,用于测量这样的简单操作,我认为它非常适合检查每种方法,您可以在这里找到:http://www.yoda.arachsys.com/csharp/benchmark.html (注意,这只是计时操作,而不是分析堆使用情况或类似内容)。

需要记住的重要一点(Jon 在上面的网站上注意到了这一点,Eric Lippert 喜欢在每次出现该主题时提到它)是,当您多次运行代码时,它只会在第一次进行 JIT,因此如果您想测量每种方法的真正差异,请至少测量两次并比较第二次运行 - 我想这是您转移订单的结果。

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).

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.

浮光之海 2024-08-15 07:33:46

尝试使用分析工具,例如 ANTS 来自 Red-Gate 或 dotTrace。分析器将向您显示每种方法所花费的时间百分比,使您能够确定哪个方法执行得更快。

Try using a profiling tool like ANTS from Red-Gate or dotTrace from JetBrains. The profilers will show you what percentage of time is spent in each method allowing you to identify which is the faster executed method.

尘曦 2024-08-15 07:33:46

学习使用您拥有的工具:

http://www.eqatec.com/tools/profiler/ 要么在循环中进行多次测试,以便

如果您得到不一致的结果,请进行科学的操作并制定控制案例:

  • 第一个案例在其余运行中进行平均
  • ,要么只测试第一个案例(重新启动每次都使用该应用程序)。

Learn to use the tools you have:

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).
后知后觉 2024-08-15 07:33:46

您可以使用 Michael McMillian 的“使用 C# 的数据结构和算法”中的 Timing 类。

大概会是这样的:

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

You can use Timing class from "Data structures and algorithms using c#" by Michael McMillian.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文