我在C++中编写了这个小程序来检查CPU负载情况。x64性能与x86相比
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
int main()
{
double x = 1;
int t1 = GetTickCount();
srand(10000);
for (unsigned long i = 0; i < 10000000; i++)
{
int r = rand();
double l = sqrt((double)r);
x *= log(l/3) * pow(x, r);
}
int t2 = GetTickCount();
printf("Time: %d\r\n", t2-t1);
getchar();
}
我在win7 x64上编译了x86和x64。
由于某种原因,当我运行x64版本时,它在大约3秒内完成运行
但是当我使用x86版本尝试它时,它花了48(!!!)秒。
我尝试了很多次,总是得到类似的结果。
什么可能导致这种差异?
什么编译器?什么优化? – GManNickG 2012-04-25 14:39:23
我也可能在最后打印'x',以确保优化器不会决定它可以丢弃'x * ='计算线。 – Rup 2012-04-25 14:40:27
我正在使用vs2010 express与“Microsoft Visual C++ 2010 Service Pack 1:Windows SDK 7.1编译器更新”。我在调试模式下将其编译为无优化。 – Idov 2012-04-25 14:41:19