clock() - execution time for c function

£可爱£侵袭症+ 提交于 2019-12-02 12:13:02

问题


I am trying to measure the execution time of a code block in C. I have something like this in my code:

clock_t begin, end;
double time_spent;
begin = clock();
ATL_dsymv(122,n,alfa,A,n,X,1,beta,Y,1);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf ("(%f seconds)",time_spent);

But it always returns: (0.000000 seconds). I tried the same thing on simpler code blocks like for's but it has the same result. What am I doing wrong? Thanks a lot.


回答1:


clock usually has very poor resolution, on the order of 10 milliseconds. This is most likely your problem. If you're on a POSIX system, use clock_gettime with the CLOCK_PROCESS_CPUTIME_ID clock to get a high-resolution result. Other types of systems probably have system-specific ways to achieve the same.



来源:https://stackoverflow.com/questions/15822933/clock-execution-time-for-c-function

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