biggdman
biggdman

Reputation: 2096

clock() - execution time for c function

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.

Upvotes: 1

Views: 868

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215259

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.

Upvotes: 4

Related Questions