【发布时间】: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。即与外部(不断变化和不可预测的)世界互动。