简体   繁体   English

测量执行时间短

[英]Measuring short execution time

time_start();

int op = 0;
for(int i = 1; i <= n; i++)
    op += arr[i]*pow(x, i);

time_stop();

This is the part of code I want to measure, time_start() and time_stop() functions just save clock() output somewhere, and then execution_time() returns the difference, giving execution time. 这是我要测量的代码部分,time_start()和time_stop()函数只是将clock()输出保存在某个地方,然后execute_time()返回差值,给出执行时间。

the problem is that for < 50000, the output is just 0ms, or 1ms. 问题是对于<50000,输出仅为0ms或1ms。 Is this correct output? 这是正确的输出吗? Can this be more exact? 这可以更精确吗?

You need to have enough "scale" between the runtime and the resolution of your timer code to get accurate and measurable results. 您需要在运行时和计时器代码的分辨率之间具有足够的“比例”,以获取准确且可测量的结果。 The simplest solution is to iterate hundreds (or thousands) of times over the small piece of code being tested. 最简单的解决方案是对要测试的一小段代码进行数百次(或数千次)迭代。

There are possible cache effects of any approach though, so be sure that you're measuring what you actually think you are. 但是,任何方法都有可能产生缓存效果,因此请确保您正在衡量自己的实际想法。

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

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