简体   繁体   中英

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.

the problem is that for < 50000, the output is just 0ms, or 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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