为什么乘法比加法还快啊 cpu

来源:百度知道 编辑:UC知道 时间:2024/09/24 05:31:38
下面的运算在我的计算机上需要738ms,当我把代码中的(i*j) 换成(i+j)
却花费了1101ms的时间,乘法在CPU中不是换成加法再运算的么,为什么反而快了呢,不解啊!
int t1 = GetTickCount();
int n;
for (int j=1; j<11; j++)
{
for (int i=0; i<100000000; i++)
{
n = (i*j)+n;
}
}
int t2 = GetTickCount();
int t3 = t2-t1;

CString strTips;
strTips.Format("%d-%d", t3, n);
AfxMessageBox(strTips);
return;

调试启动,然后看汇编代码就知道了。。应该是编译器进行了优化。

我的情况R版
加:16xx ms
乘:2100 ms

D版
加:36xx ms
乘:4200 ms

CPU:Athlon XP 2500+ OC 3200+

lz的CPU真厉害,不但时间短,乘法还比加法快 - -!

那你的cpu反着算呗~