【问题标题】:Why does perf -e cpu-cycles report different answers on multiple runs?为什么 perf -e cpu-cycles 在多次运行时报告不同的答案?
【发布时间】:2016-07-31 17:04:51
【问题描述】:

关于这个示例代码:

#include <iostream>
using namespace std;

int main()
{
    cout<<"Hello World!"<<endl;

    return 0;
}

我运行了以下命令 3 次:

perf stat -e cpu-cycles ./sample

以下是连续执行的 3 个输出:

1)

Hello World!

Performance counter stats for './try':

     22,71,970      cpu-cycles               

   0.003634105 seconds time elapsed

2)

Hello World!

 Performance counter stats for './try':

     18,51,044      cpu-cycles               

   0.001045616 seconds time elapsed

3)

Hello World!

 Performance counter stats for './try':

     18,21,834      cpu-cycles               

   0.001153489 seconds time elapsed

为什么同一个程序在多次运行时会占用不同数量的 CPU 周期?

我正在使用“Intel(R) Core(TM) i5-5250U CPU @ 1.60GHz”、“Ubuntu 14.04.3 LTS”和“g++ 4.8.4”。

【问题讨论】:

  • 我认为这个问题必须重新打开。链接的问题主要集中在sleep 的特殊性上。我相信在这种情况下,变异有不同的解释。
  • 一方面,您的程序主要执行 I/O。即与外部(不断变化和不可预测的)世界互动。

标签: c++ linux g++ perf


【解决方案1】:

在程序启动期间,二进制代码是内存映射的,但会延迟加载(根据需要)。因此,在程序的第一次调用期间,内核会花费一些 CPU 周期来(透明地)加载二进制代码,因为执行会遇到尚未在 RAM 中的指令页。后续调用需要更少的时间,因为它们重用了缓存的代码,除非底层文件已更改或程序长时间未执行并且其页面被回收。

【讨论】:

    猜你喜欢
    • 2023-03-28
    • 1970-01-01
    • 2014-05-07
    • 2013-01-18
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多