微基准测试需要运行多长时间?

发布于 2024-09-01 14:49:06 字数 523 浏览 3 评论 0原文

首先,这不是关于微基准测试的有用性。我很清楚他们的目的:在非常具体的情况下指示性能特征和比较,以突出一个方面。这是否会对你的工作产生任何影响则是另一回事了。

几年前,有人(我想是 Heinz Kabutz?)指出,每个值得花时间查看其结果的基准测试都必须运行至少几分钟,并且需要运行至少 3 次,而第一次运行是总是被丢弃。这是为了解释 JVM 的预热以及环境中的不一致(后台进程、网络流量……)和测量不准确。这对我来说很有意义,而且我的个人经历也暗示了类似的情况,所以我总是采用这种策略。

然而,我注意到很多人(例如 Jeff)编写仅运行几毫秒(!)并且仅运行一次的基准测试。我知道近年来短期运行基准的准确性有所提高,但我仍然觉得很奇怪。难道每个微基准测试不应该运行至少一秒并至少运行 3 次才能获得有用的输出吗?或者这个规则现在已经过时了吗?

First of all, this is not about the usefulness of microbenchmarks. I'm well aware of their purpose: Indicating performance characteristics and comparison in a very specific case to highlight a single aspect. Whether or not this should have any implications on your work is a different story.

A few years ago, someone (I think Heinz Kabutz?) noted that every benchmark that is worth the time to look at its results has to run at least a few minutes and needs to be run at least 3 times, whereas the first run is always discarded. That was to account for warming up the JVM as well as inconsistencies in the environment (background processes, network traffic, ...) and measurement inaccuracies. That made sense to me, and my personal experiences suggested something similar, so I always adopted this strategy.

However, I noticed many people (for instance Jeff) write benchmarks that only run for a couple milliseconds (!) and are run only once. I know that the accuracy of short-running benchmarks went up in the recent years, but it still strikes me as odd. Shouldn't every microbenchmark run for at least a second and be run at least 3 times to get a somewhat useful output? Or is that rule obsolete nowadays?

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

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

发布评论

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

评论(1

鼻尖触碰 2024-09-08 14:49:06

根据我的经验,您需要:

  • 运行多次(并丢弃第一个结果 - VM 和其他效果)
  • 如果您正在考虑运行足够长的计算密集型代码
  • 以降低成本,则 需要花费最短时间循环和计时函数的
  • 理想情况是在一个操作系统时间片(通常为 10 毫秒)内运行超过一个时间片,例如运行约 5 毫秒或约 500 毫秒。

我只倾向于使用计算密集型代码 - 如果您有不同的配置文件(例如内存密集型或大量 I/O),那么计时策略可能需要不同。

In my experience you need to:

  • run multiple times (and discard the first result - VM and other effects)
  • take the minimum time if you're looking at compute-intensive code
  • run for long enough to mitigate cost of loops and timing functions
  • ideally run within one OS time slice (typically 10 ms) or for much more than one time slice, e.g. run for ~5 ms or ~500 ms.

I only tend to work with compute-intensive code - if you have a different profile (e.g. memory-intensive, or lots of I/O) then the timing strategy may need to be different.

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