通过位移位快速乘法提高性能
我最近阅读了很多有关编程实践、设计等的文章,并且对将乘法实现为位移位所带来的实际性能收益感到好奇。
我读到的例子是鼓励将 x*320 实现为 (x<<8 + x<<6) 作为常用例程。
这在现代编译器中有多重要?如果有显着的性能提升,编译器是否可以根据需要自动将这些“简单乘法”转换为位移位?
有没有人在他们的项目中不得不采用这种方式进行位移来实现更快的乘法?您可以期望获得哪些性能提升?
I have been reading a lot of articles lately about programming practice, design and so forth and was curious about the real performance gains from implementing multiplication as bit shifting.
The example I was reading about was encouraging implementing x*320 as (x<<8 + x<<6) for a commonly used routine.
How relevant is this in modern compilers? If there are significant performance gains, can compilers not automatically convert these "easy multiplications" to bit-shifts as necessary?
Has anyone had to resort to bit-shifting in this way in their projects to achieve faster multiplication? What performance gains can you expect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,编译器会为您完成大部分工作。他们对此也非常有侵略性。所以很少需要自己做。 (特别是以可读性为代价)
但是,在现在的现代机器上,乘法并不比移位慢“那么”。因此,任何需要超过 2 次移位的数字最好使用乘法来完成。编译器知道这一点并会做出相应的选择。
编辑:
根据我的经验,我在这方面从来无法超越编译器,除非代码通过 SSE 内在函数进行矢量化(编译器并没有真正尝试优化)。
Yes, compilers will do most of these for you. They're pretty aggressive with it too. So there's rarely a need to do it yourself. (especially at the cost of readability)
However, on modern machines now, multiplication isn't "that" much slower than shifts. So any number that needs more than like 2 shifts are better done using multiplication. The compilers know this and will choose accordingly.
EDIT:
From my experience, I've never been able to outdo a compiler in this area unless the code was vectorized via SSE intrinsics (which the compilers don't really try to optimize).