Execution time in nano seconds and related issues

Posted by anup on Stack Overflow See other posts from Stack Overflow or by anup
Published on 2010-05-27T11:31:04Z Indexed on 2010/05/27 11:41 UTC
Read the original article Hit count: 417

Filed under:

Hi All,

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...

© Stack Overflow or respective owner

Related posts about c