如何评估程序的运行时? [英] How to evaluate a program's runtime?
问题描述
我已经开发了一个简单的程序,并希望在实际计算机上评估其运行时性能,例如我的MacBook. 源代码为:
I've developed a simple program and want to evaluate its runtime performance on a real machine, e.g. my MacBook. The source code goes:
#include <stdio.h>
#include <vector>
#include <ctime>
int main () {
auto beg = std::clock () ;
for (int i = 0; i < 1e8; ++ i) {
}
auto end = std::clock () ;
printf ("CPU time used: %lf ms\n", 1000.0*(end-beg)/CLOCKS_PER_SEC) ;
}
它使用gcc编译,并且优化标志设置为默认值. 在bash脚本的帮助下,我运行了1000次,并通过MacBook记录了运行时间,如下所示:
It's compiled with gcc and the optimization flag is set to the default. With the help of bash script, I ran it for 1000 times and recorded the runtime by my MacBook, as following:
[130.000000, 136.000000): 0
[136.000000, 142.000000): 1
[142.000000, 148.000000): 234
[148.000000, 154.000000): 116
[154.000000, 160.000000): 138
[160.000000, 166.000000): 318
[166.000000, 172.000000): 139
[172.000000, 178.000000): 40
[178.000000, 184.000000): 11
[184.000000, 190.000000): 3
"[a,b):n"表示同一程序的实际运行时间在a毫秒与b毫秒之间,持续n次.
"[a, b): n" means that the actual runtime of the same program is between a ms and b ms for n times.
很明显,实际运行时变化很大,并且似乎不是正态分布.有人可以告诉我是什么原因造成的,以及如何正确评估运行时间?
It's clear that the real runtime varies greatly and it seems not a normal distribution. Could someone kindly tell me what causes this and how I can evaluate the runtime correctly?
感谢您回答这个问题.
推荐答案
基准化很难!
简短答案:使用 Google基准
长答案: 有很多事情会影响时间安排.
Long answer: There are many things that will interfere with timings.
- 计划(操作系统代替您运行其他事情)
- CPU扩展(操作系统决定通过降低运行速度来节省能源)
- 内存争用(您需要时使用内存的其他方式)
- 总线争用(与您要交谈的设备通话的其他方式)
- 缓存(CPU保留一个值以避免使用内存)
- CPU迁移. (操作系统将您从一个CPU转移到另一个CPU)
- 时钟不准确(只有CPU时钟在任何程度上都是准确的,但是如果您迁移,它们会发生变化)
避免这些影响的唯一方法是禁用CPU扩展,执行缓存刷新"功能(通常在启动前仅触摸大量内存),高优先级运行以及将自己锁定在单个CPU上.即使如此,您的时间安排仍然很嘈杂,所以最后一件事就是简单地重复很多,并使用平均值.
这就是为什么 google基准之类的工具可能是您最好的选择.
The only way to avoid these effects are to disable CPU scaling, to do "cache-flush" functions (normally just touching a lot of memory before starting), running at high priority, and locking yourself to a single CPU. Even after all that, your timings will still be noisy, so the last thing is simply to repeat a lot, and use the average.
This why tools like google benchmark are probably your best bet.
video from CPPCon
Also available live online
这篇关于如何评估程序的运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!