开发者

I need better solution to get time of execution program in C

I am using this

 clock_t start = clock();

    开发者_JAVA技巧QuickSort(0, ItemCount-1,1);

 printf("Time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC);

I would like to get time in nanoseconds.


If you're on a Unix-like system you can most likely use the clock_gettime() function. In Linux, it is found in -lrt, most other systems I've tried have it in the system C library.

It should be noted that x86 has an rdtsc instruction that is extremely precise.


Use the struct timespec as follows to obtain the time of execution of the code in nanoseconds precision

struct timespec start, end;
clock_gettime(CLOCK_REALTIME,&start);
/* Do something */
clock_gettime(CLOCK_REALTIME,&end);

It returns a value as ((((unsigned64)start.tv_sec) * ((unsigned64)(1000000000L))) + ((unsigned64)(start.tv_nsec))))

Hope this answer will be more helpful for you to get your desired execution time in nanoseconds.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜