问题
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