在此示例代码中:
#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 elapsed2)
Hello World!
Performance counter stats for './try':
18,51,044 cpu-cycles
0.001045616 seconds time elapsed3)
Hello World!
Performance counter stats for './try':
18,21,834 cpu-cycles
0.001153489 seconds time elapsed为什么同一个程序会在多次运行中占用不同数量的cpu周期?
我使用的是“英特尔(R)酷睿(TM)i5-5250UCPU@1.60 g++”、"Ubuntu 14.04.3 LTS“和"g++ 4.8.4”。
发布于 2016-08-01 14:55:44
在程序启动期间,二进制代码是内存映射的,但延迟加载(根据需要)。因此,在程序的第一次调用期间,当执行命中还不在RAM中的指令页时,内核会花费一些CPU周期(透明地)加载二进制代码。后续调用所需的时间较少,因为它们重用了缓存的代码,除非底层文件发生了更改,或者程序很长时间没有执行,并且其页面被回收。
https://stackoverflow.com/questions/38686414
复制相似问题