Understanding about clock() and CLOCKS_PER_SEC in C++
我对准确计时c应用程序感兴趣。"时间"似乎有多种定义,但是出于这个问题的考虑……我对我指望现实世界中的手表时间感兴趣……如果有任何意义的话!无论如何,在我的应用程序中,我的开始时间是这样完成的:
1 2 3 4 5 6 7 8 9 10 11 12 13 | clock_t start = clock(); . . . // some statements . . clock_t end = clock(); . . double duration = end - start; . . cout << CLOCKS_PER_SEC << endl; |
开始等于184000
结束等于188000
CLOCKS_PER_SEC等于1000000
这是否意味着持续时间(以秒为单位)等于4000/1000000?如果是这样,那意味着持续时间是0.004秒?有没有更准确的方法来衡量这一点?
谢谢
尝试以纳秒精度查找时间
1 2 3 4 | struct timespec start, end; clock_gettime(CLOCK_REALTIME,&start); /* Do something */ clock_gettime(CLOCK_REALTIME,&end); |
它返回值为
如果您认为有帮助,也请参考此链接。.