简体   繁体   English

测量计算时间

[英]Measuring computation time

I have a program that works with iterations. 我有一个与迭代一起使用的程序。 For example, if I have 50000 iterations, the program does 50000 iterations, and finally stops. 例如,如果我有50000次迭代,则程序进行50000次迭代,最后停止。 When I measure computation time for 1000 iterations, I got (for example) 1 second, but for 50000 iterations, 10 seconds. 当我测量1000次迭代的计算时间时,我得到了(例如)1秒,但是对于50000次迭代,则为10秒。

Should I expect a direct ratio with iteration numbers and computation time? 我应该期望迭代次数和计算时间成正比吗? For the example given, shouldn't I expect 50 seconds for 50000 iterations? 对于给定的示例,我不应该期望50秒钟进行50000次迭代吗? I'm just confused with the results… 我只是对结果感到困惑...

I am measuring it with using clock() function. 我正在使用clock()函数对其进行测量。 Before iteration i start with srand(time(NULL)); 在迭代之前,我从srand(time(NULL))开始。 than define clock_t startTime; 比定义clock_t startTime; startTime = clock(); startTime = clock(); ..... and measure final iteration time with ((double)(clock() - startTime)) / CLOCKS_PER_SEC): .....并使用((double)(clock()-startTime))/ CLOCKS_PER_SEC)测量最终迭代时间:

Measuring performance can be tricky on modern processors because for example many of them have a variable clock speed. 在现代处理器上,测量性能可能很棘手,因为例如其中许多处理器的时钟速度可变。

This means that they're normally going slow because no CPU is needed and they can use less energy and generate less heat that way. 这意味着它们通常会变慢,因为不需要CPU,并且它们可以消耗更少的能量并以这种方式产生更少的热量。

When you require an heavy computation the OS detects that now there is work for the CPU and can increase the clock speed (and heat and current) to get the results faster. 当您需要大量计算时,操作系统会检测到CPU现在可以工作了,可以提高时钟速度(以及热量和电流)以更快地获得结果。

To avoid this specific problem you should measure clock cycles used by your computation instead of cpu time (or worse wall clock time). 为避免出现此特定问题,您应该测量计算中使用的时钟周期,而不是cpu时间(或更糟糕的挂钟时间)。 A good profiler should give you this option (I'm using oprofile ). 一个好的分析器应该给您这个选项(我正在使用oprofile )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM