1.源代码:这里单位用毫秒。测试程序是两个for循环。
#include<stdio.h>
#include<sys/time.h>
struct timeval tv;
int main()
{
int count = 0;
gettimeofday(&tv, NULL);
printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); //毫秒
for(int i = 0; i < 50005; i++)
{
for(int j = 0; j < 50005; j++)
{
count++;
}
}
gettimeofday(&tv, NULL);
printf("millisecond:%ld\n",tv.tv_sec*1000 + tv.tv_usec/1000); //毫秒
return 0;
}
2.运行结果: