I am using the following code to compute execution time in milli-secs.
struct timespec tp;
if (clock_gettime (CLOCK_REALTIME, &tp) == 0)
return ((tp.tv_sec * 1000000000) + tp.tv_nsec);
else
return ;
Can you please tell me whether this is correct?
Let’s name this function comptime_nano().
Now, I write the following code in main() to check execution times of following operations.
unsigned long int a, b, s1, s3;
a = (unsigned long int)(1) << 63;
b = (unsigned long int)(1) << 63;
btime = comptime_nano();
s1 = b >> 30;
atime = comptime_nano();
printf ("Time =%ld for %lu\n", (atime - btime), s1);
btime = comptime_nano();
s3 = a >> 1;
atime = comptime_nano();
printf ("Time =%ld for %lu\n", (atime - btime), s3);
To my surprise, the first operation takes about roughly 4 times more time than the second. Again, if I change the relative ordering of these operations, the respective timings change drastically.
Please comment…
clock_gettimeis not accurate enough for that kind of measurement. If you need to measure operations like that, do the operation several thousand (or several million times) in a loop before comparison. The two operations above should take the same amount of time but the second in your example code does not have the overhead of loadinga,b,s1, ands3into the processor’s cache.Also, what’s going on here?
The first return is illegal if the function returns
void, and the second is illegal if it does not returnvoid….EDIT:
1000000000also overflows the range ofint.