C how to measure time correctly?

為{幸葍}努か 提交于 2020-01-11 05:12:08

问题


This is the "algorithm", but when I want to measure the execution time it gives me zero. Why?

#define ARRAY_SIZE 10000
...

clock_t start, end;

start = clock();

for( i = 0; i < ARRAY_SIZE; i++) 
{
non_parallel[i] = vec[i] * vec[i];
}
end = clock();
printf( "Number of seconds: %f\n", (end-start)/(double)CLOCKS_PER_SEC );

So What should i do to measure the time?


回答1:


Two things:

  1. 10000 is not a lot on a modern computer. Therefore that loop will run in probably less than a millisecond - less than the precision of clock(). Therefore it will return zero.

  2. If you aren't using the result of non_parallel its possible that the entire loop will be optimized out by the compiler.

Most likely, you just need a more expensive loop. Try increasing ARRAY_SIZE to something much larger.


Here's a test on my machine with a larger array size:

#define ARRAY_SIZE 100000000

int main(){

    clock_t start, end;

    double *non_parallel = (double*)malloc(ARRAY_SIZE * sizeof(double));
    double *vec          = (double*)malloc(ARRAY_SIZE * sizeof(double));

    start = clock();

    for(int i = 0; i < ARRAY_SIZE; i++) 
    {
        non_parallel[i] = vec[i] * vec[i];
    }

    end = clock();
    printf( "Number of seconds: %f\n", (end-start)/(double)CLOCKS_PER_SEC );


    free(non_parallel);
    free(vec);
    return 0;
}

Output:

Number of seconds: 0.446000



回答2:


This is an unreliable way to actually time number of seconds, since the clock() function is pretty low precision, and your loop isn't doing a lot of work. You can either make your loop do more so that it runs longer, or use a better timing method.

The higher precision methods are platform specific. For Windows, see How to use QueryPerformanceCounter? and for linux see High resolution timer with C++ and Linux?



来源:https://stackoverflow.com/questions/10154962/c-how-to-measure-time-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!