用C测试加减乘除压力测试

Posted: 2010年6月8日星期二
闲得无聊写了一个计算一百万次加减乘除所需要的时间,具体代码如下:

#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 评论: