#include <stdio.h>
#include <sys/time.h>
#define MAX 1000000
static double currtime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return 0.0;
return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
}
int main(int argc, char *argv[])
{
int i, j = 0;
double stime, etime;
stime = currtime();
for (i = 0; i < MAX; i++)
j += 0;
etime = currtime();
printf("%d time's plus exec time is:\t%f\n", MAX, etime - stime);
stime = currtime();
for (i = 0; i < MAX; i++)
j -= 0;
etime = currtime();
printf("%d time's subtracting exec time is:\t%f\n", MAX, etime - stime);
stime = currtime();
for (i = 0; i < MAX; i++)
j *= 1;
etime = currtime();
printf("%d time's multiplication exec time is:\t%f\n", MAX, etime - stime);
stime = currtime();
for (i = 0; i < MAX; i++)
j /= 1;
etime = currtime();
printf("%d time's division exec time is:\t%f\n", MAX, etime - stime);
return 0;
}
在我的笔记本上测试,测试出来的加减乘除执行一百万次所需要的时间基本差不多。
0 评论:
发表评论