哪种性能更好:乘法或除法? [英] What has a better performance: multiplication or division?

查看:126
本文介绍了哪种性能更好:乘法或除法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪个版本更快?
x * 0.5

x / 2



我上过课程前一段时间在大学里叫计算机系统。从那时起,我记得可以用相当简单的逻辑门来实现两个值的乘积,但是除法不是本机运算,而是需要一个除数在一个循环中的总和寄存器,该除数与除数相加。 p>

现在我必须优化具有很多除法的算法。不幸的是,它不只是被二除,所以二进制移位是不可能的。



update:



我更改了代码,并且没有发现任何区别。您可能对编译器的优化是正确的。由于所有答案都很棒,因此我全都反对了。我选择了rahul的答案,因为它的链接很好。

解决方案

好吧,如果只是一次计算,您几乎不会注意到任何区别,但是如果您谈论数百万笔交易,那么Division无疑是比乘法昂贵。您始终可以使用最清晰易读的内容。



请参考以下链接:-我应该使用乘法还是除法?


Which version is faster ? x * 0.5 or x / 2

Ive had a course at the university called computer systems some time ago. From back then i remember that multiplying two values can be achieved with comparably "simple" logical gates but division is not a "native" operation and requires a sum register that is in a loop increased by the divisor and compared to the dividend.

Now i have to optimise an algorithm with a lot of divisions. Unfortunately its not just dividing by two so binary shifting is no option. Will it make a difference to change all divisions to multiplications ?

update:

I have changed my code and didnt notice any difference. You're probably right about compiler optimisations. Since all the answers were great ive upvoted them all. I chose rahul's answer because of the great link.

解决方案

Well if it is a single calculation you wil hardly notice any difference but if you talk about millions of transaction then definitely Division is costlier than Multiplication. You can always use whatever is the clearest and readable.

Please refer this link:- Should I use multiplication or division?

这篇关于哪种性能更好:乘法或除法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆