方法一:
#include<time.h>
clock_t start = clock();
需要计时的代码段
clock_t end = clock();
运行时间t = end - start; (单位ms)
本方法有一定缺陷,在32bit机器上,运行时间较长达到(超过1小时),有可能出现计时错误。
clock()文档说明如下:
Note that the time can wrap around. On a 32-bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes.
因此,当出现计时为负数或者很小的数时,需要人为凭经验加上若干个72minutes。。。
方法二:
显然方法一有一定局限性,方法二解决了长时间计时的问题。
#include<time.h>
timeval start, end;
gettimeofday(&start, null);
需要计时的代码段
gettimeofday(&end, null);
运行时间t = 1000*(end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec)/1000; (单位ms)
本文出自:http://2xvision.blog.163.com/blog/static/21803373200942763542621/