对CPU约束的算法/实现进行基准测试 [英] Benchmarking CPU-bound algorithms/implementations

查看:85
本文介绍了对CPU约束的算法/实现进行基准测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在用一种编译语言(例如C ++)编写自己的StringBuilder.

Let's say I'm writing my own StringBuilder in a compiled language (e.g. C++).

衡量各种实现的最佳方法是什么?仅仅对数十万次运行进行计时会产生高度不一致的结果:一批之间的计时差异可能高达15%,因此无法准确评估潜在的性能改进,从而获得的性能增益要小得多.

What is the best way to measure the performance of various implementations? Simply timing a few hundred thousand runs yields highly inconsistent results: the timings from one batch to the other can differ by as much as 15%, making it impossible to accurately assess potential performance improvements that yield performance gains smaller than that.

我已完成以下操作:

  1. 禁用SpeedStep
  2. 使用RDTSC进行计时
  3. 以实时优先级运行流程
  4. 将关联性设置为单个CPU内核

这使结果有些稳定.还有其他想法吗?

This stabilizied the results somewhat. Any other ideas?

推荐答案

精确测量一段代码确实很困难.对于此类要求,建议您查看 Agner Fog的测试套件.通过使用它,您可以测量时钟周期并收集一些重要因素(例如缓存未命中,分支错误预测等).

It's really hard to precisely measure a piece of code. For such requirements, I recommend you to have look at Agner Fog's test suite. By using it, you can measure clock cycles and collect some important factors (such as cache misses, branch mispredictions etc.).

此外,我建议您查看Agner网站上的 PDF文档.这是实现如此微优化的非常宝贵的文件.

Also, I recommend you to have look at PDF document from Agner's site. It's a really invaluable document to make possible such micro-optimization.

请注意,实际性能不是时钟周期"的函数.高速缓存未命中可以更改实际应用程序中每次运行的所有内容.因此,我将首先优化缓存未命中.只需为同一内存部分运行几次代码,就可以大大减少缓存丢失.因此,很难进行精确测量.整个应用程序调整通常是IMO更好的主意.英特尔VTune和其他工具确实非常适合此类用途.

As a side note, actual performance is not a function of "clock cycles". Cache misses can change everything for each run within a real application. So, I would optimize cache misses first. Simply running a piece of code several times for same memory portion, decreases cache miss dramatically. So, it makes it hard to measure precisely. Whole application tuning is usually better idea IMO. Intel VTune and other tools are really good for such usages.

这篇关于对CPU约束的算法/实现进行基准测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆