如何准确测量c ++函数使用的时钟周期? [英] How to accurately measure clock cycles used by a c++ function?

查看:89
本文介绍了如何准确测量c ++函数使用的时钟周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我必须使用:rdtsc.测得的函数是确定性的,但结果远非可重复(每次运行都会产生5%的振荡). 可能的原因是:

I know that I have to use: rdtsc. The measured function is deterministic but the result is far from being repeatable (I get 5% oscillations from run to run). Possible causes are:

  • 上下文切换
  • 缓存未命中

您知道其他原因吗? 如何消除它们?

Do you know any other causes? How to eliminate them?

推荐答案

TSC( rdtsc 的用途)通常在多处理器系统上不同步.为了将进程绑定到单个CPU,可能有助于设置CPU亲和力.

TSCs (what rdtsc uses) are often not synchronized on multi-processor systems. It may help to set the CPU affinity in order to bind the process to a single CPU.

如果可能,您还可以从 HPET计时器获取时间戳,该时间戳不容易同样的问题.

You could also get timestamps from HPET timers if available, which aren't prone to the same problem.

关于可重复性,这些差异是正确的.您可以禁用缓存,为进程提供实时优先级,和/或(如果在Linux或类似系统上)以较低的固定计时器中断频率(进行时间分割的频率)重新编译内核.您无法完全消除差异,至少不能轻易消除这种差异,并且不能在常规的CPU + OS组合上实现.

As for repeatability, those variances are true. You could disable caching, give a realtime priority to the process and/or (if on Linux or something similar) recompile your kernel with a lower, fixed timer interrupt frequency (the one that does time-slicing). You can't eliminate the variance completely, at least not easily and not on regular CPU + OS combos.

通常,出于易于编码,可靠性和可移植性的原因,建议您使用操作系统提供的功能.如果它提供高精度计时器,请使用适当的OS帮助程序.

In general, for easy coding, reliability and portability reasons, I suggest you use what the OS has to offer. If it offers high-precision timers, use the appropriate OS helper.

((如果您要在加密系统上尝试进行时间攻击,那么,您将不得不忍受1.这种随机性和2.一般防御措施,由于种种原因使系统无法预测,因此,功能可能无法确定时间.)

(Just in case you're trying a time attack on a crypto system, well, you're going to have to live with 1. this randomness and 2. general defenses that make the system unpredictable for good reasons, so the function might not be deterministic with respect to time.)

添加了有关操作系统可以提供的计时器的段落.

added paragraph about timers the OS can offer.

这是指Linux.要将进程绑定到单个CPU(以准确读取RDTSC),可以使用 sched_setaffinity (2).并且这里是我的一个项目中的一些代码,将其用于其他目的(将线程映射到CPU).这应该是您的第一次尝试.对于HPET,您可以使用常规的POSIX调用,例如这些,只要内核和计算机支持这些计时器.

This refers to Linux. For binding a process to a single CPU (to have an accurate read from RDTSC), you can use sched_setaffinity(2). And here's some code from one of my projects using it for some other purpose (mapping threads to CPUs). This should be your first try. As for HPETs, you can use regular POSIX calls like these, as long as the kernel and the machine support these timers.

这篇关于如何准确测量c ++函数使用的时钟周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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