#include <stdio.h>
#include <time.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#ifdef WIN32
int gettimeofday(struct timeval *tp, void *tzp)
{
time_t clock;
struct tm tm;
SYSTEMTIME wtm;
GetLocalTime(&wtm);
tm.tm_year = wtm.wYear - 1900;
tm.tm_mon = wtm.wMonth - 1;
tm.tm_mday = wtm.wDay;
tm.tm_hour = wtm.wHour;
tm.tm_min = wtm.wMinute;
tm.tm_sec = wtm.wSecond;
tm.tm_isdst = -1;
clock = mktime(&tm);
tp->tv_sec = clock;
tp->tv_usec = wtm.wMilliseconds * 1000;
return (0);
}
#endif
int main()
{
float time_use = 0;
struct timeval start;
struct timeval end;
gettimeofday(&start, NULL); //gettimeofday(&start,&tz);结果一样
printf("start.tv_sec:%d\n", start.tv_sec);
printf("start.tv_usec:%d\n", start.tv_usec);
int i = 100000000;
while (i--);
gettimeofday(&end, NULL);
printf("end.tv_sec:%d\n", end.tv_sec);
printf("end.tv_usec:%d\n", end.tv_usec);
time_use = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec);//微秒
printf("time_use is %.10f\n", time_use);
system("pause");
}
计算耗时
最新推荐文章于 2023-01-09 13:37:43 发布