C/C++加减乘除运算效率分析

39 篇文章 14 订阅

运算效率

移位 > 赋值 > 大小比较 > 加法 > 减法 > 乘法 > 取模 > 除法。

使用移位来代替乘除法,提高运算效率

	int a = 64;
	cout << "a / 2 = " << (a >> 1) << endl;
	cout << "a / 4 = " << (a >> 2) << endl; 
	cout << "a / 8 = " << (a >> 3) << endl;
	cout << "a * 2 = " << (a << 1) << endl;
	cout << "a * 4 = " << (a << 2) << endl;
	cout << "a * 8 = " << (a << 3) << endl;

/*
a / 2 = 32
a / 4 = 16
a / 8 = 8
a * 2 = 128
a * 4 = 256
a * 8 = 512
*/

加法比减法快,乘法比除法快原因是计算机硬件只能做加法,CPU里面都是加法器
计算机中有专门的移位功能部件,这也是最基础的部件。乘法和除法都是靠移位实现的。乘2^n,左移n位,除2^n,右移n位

另外,原始的乘法器是一步一步乘(移位)出来的,每次取乘数的一位与被乘数操作,1则把被乘数照写,0则为0,然后乘数右移。这样循环,最后把每一步结果加起来。
后面通过阵列连乘器改进速度,一次算出上面每一步的结果,然后直接相加。

乘法是加操作,而除法是每步的结果作加法或减法(加减交替法),有的算法还需要恢复上一次的结果(余数恢复法),而且每一步加减后还要进行移位,所以最慢。

从数学上讲,CPU中的ALU在算术上只干了两件事,加法,移位,顶多加上取反,在逻辑上,只有与或非异或。

加法->加法。

减法->取反,加法。

乘法->移位,逻辑判断,累加

除法->移位,逻辑判断,累减

至于乘法除法为什么这样,搜索二进制数如何进行乘法除法,说白了,和我们熟悉的十进制运算流程上一模一样。

所以只需要加法器,移位器和基本逻辑门电路就构成一个简单的ALU。

从硬件实现上讲,可以看出,实现这四种基本运算只需要上述硬件组件就行。早期的cpu里结构简单,只有这些组件,没有专门的乘法器、除法器。但是可想而知效率也是低下的(其中除法效率最低,因为每次移位后比乘法还多出一次试错操作),后来的cpu会集成专门的并行处理电路在cpu内建的协处理器(比如浮点运算器,很早的cpu是没有专门计算浮点的电路的)中,在硬件上实现,这样计算速度就快了。当然,计算的逻辑还是没变。

  • 11
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是 C++ 有理数的加减乘除运算示例代码: ```c++ #include <iostream> using namespace std; class Rational { private: int numerator; // 分子 int denominator; // 分母 public: // 构造函数 Rational(int numerator = 0, int denominator = 1) { if (denominator == 0) { cout << "Error: denominator cannot be 0!" << endl; exit(1); } if (numerator == 0) { this->numerator = 0; this->denominator = 1; } else { int gcd = getGCD(numerator, denominator); this->numerator = numerator / gcd; this->denominator = denominator / gcd; if (this->denominator < 0) { this->numerator = -this->numerator; this->denominator = -this->denominator; } } } // 获取最大公约数 int getGCD(int a, int b) { if (b == 0) return a; return getGCD(b, a % b); } // 加法运算 Rational operator+(Rational const& r) { int newNumerator = this->numerator * r.denominator + r.numerator * this->denominator; int newDenominator = this->denominator * r.denominator; return Rational(newNumerator, newDenominator); } // 减法运算 Rational operator-(Rational const& r) { int newNumerator = this->numerator * r.denominator - r.numerator * this->denominator; int newDenominator = this->denominator * r.denominator; return Rational(newNumerator, newDenominator); } // 乘法运算 Rational operator*(Rational const& r) { int newNumerator = this->numerator * r.numerator; int newDenominator = this->denominator * r.denominator; return Rational(newNumerator, newDenominator); } // 除法运算 Rational operator/(Rational const& r) { if (r.numerator == 0) { cout << "Error: division by 0!" << endl; exit(1); } int newNumerator = this->numerator * r.denominator; int newDenominator = this->denominator * r.numerator; return Rational(newNumerator, newDenominator); } // 输出有理数 friend ostream& operator<<(ostream& os, Rational const& r) { if (r.denominator == 1) { os << r.numerator; } else { os << r.numerator << "/" << r.denominator; } return os; } }; int main() { Rational a(1, 2); Rational b(3, 4); Rational c = a + b; Rational d = a - b; Rational e = a * b; Rational f = a / b; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "a + b = " << c << endl; cout << "a - b = " << d << endl; cout << "a * b = " << e << endl; cout << "a / b = " << f << endl; return 0; } ``` 输出结果: ``` a = 1/2 b = 3/4 a + b = 5/4 a - b = -1/4 a * b = 3/8 a / b = 2/3 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SOC罗三炮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值