前言
主要是为了统计下某段程序的运行时间
代码实现
主要调用了linux c下的<sys/time.h>
#include<stdio.h>
#include<sys/time.h>double tick(void)
{struct timeval t;gettimeofday(&t, 0);return t.tv_sec + 1E-6 * t.tv_usec;
}int main(int argv, char* argc[])
{double t = tick();for (int i = 0; i < 10000000; i++){;}t = tick() - t;printf("Completed in %.3f secs \n", t);return 0;
}
输出
Completed in 0.028 secs