以不寻常但有效的方式做某事
我今天看了一个视频,视频中的人只是写了这个来了解数字是否为偶数:
number/2*2 == number ? true : false ;
我回家后尝试了一下,并与
number % 2 == 0 ? true : false ;
第二个相比,第二个更快,然后我将第一个更改为:
number>>1<<1 == number ? true : false;
这次时间平移向右一次和向左一次的数字工作得更快:D 性能差异并不大,只需 0-1 秒即可识别所有数字 1 到 1000000000 之间,但我非常喜欢它并且想听听你的这些技巧。
那么还有什么? =)
和 Russell Borogove 的另一个想法 =)
(number&1) == 0;
结果:
And 操作所花费的时间:00:00:07.0504033
移位操作所用时间:00:00:06.4653698
Mod 操作所用时间:00:00:06.8323908
令人惊讶的是,在我的计算机上,两次移动比单次移动速度更快。
I watched a video today and the guy in the video just write this to understand whether a number is even or not:
number/2*2 == number ? true : false ;
i tried it when i got home and compared with
number % 2 == 0 ? true : false ;
The second one was faster then i changed the first one as:
number>>1<<1 == number ? true : false;
this time shifting the number once to the right and once to left worked faster :D
The performance difference is not huge just 0-1 second for identifying all the numbers
between 1 and 1000000000 but I liked it very much and wanted to hear such tricks from you.
so what else ? =)
and another idea from Russell Borogove =)
(number&1) == 0;
Results:
Time Elapsed With And Operation:00:00:07.0504033
Time Elapsed With Shift Operation:00:00:06.4653698
Time Elapsed With Mod Operation:00:00:06.8323908
Surprisingly shifting two times is working faster than a single and operation on my computer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
麻省理工学院实际上保留了此类事物的列表,HAKMEM,可以在 http:// /www.inwap.com/pdp10/hbaker/hakmem/hakmem.html。大多数与编程相关的内容都是用汇编语言编写的,但我知道其中一些已被翻译为 C,位于 http://graphics.stanford.edu/~seander/bithacks.html。
现在进行讲座:这些肮脏的技巧可能会更快,但需要很长时间才能理解。
大多数计算对性能的要求并不高,因此不需要像这样的技巧。在奇偶情况下,
number % 2 == 0
比number/2*2 == number
或number>>> 更清晰、更具可读性。 1<<1==数字
。也就是说,在正常应用程序中,您应该始终使用更简单、更标准的选项,因为它将使您的代码更易于理解和维护。然而,像这样的技巧是有用例的。特别是在大规模数学或科学计算和计算机图形学中,这样的技巧可以挽救你的生命。一个很好的例子是 John Carmack 在《雷神之锤 3》中的“魔法逆平方根”。
MIT actually keeps a list of such things, HAKMEM, which can be found at http://www.inwap.com/pdp10/hbaker/hakmem/hakmem.html. Most of the programming-related ones are written in assembly language, but I understand that some of them have been translated to C at http://graphics.stanford.edu/~seander/bithacks.html.
Now for a lecture: These dirty tricks might be faster, but take far too long to comprehend.
Most computing isn't so performance-critical that tricks like this are necessary. In the odd-even case,
number % 2 == 0
is much clearer and more readable thannumber/2*2 == number
ornumber>>1<<1 == number
. That said, in normal applications you should always use the simpler and more standard option because it will make your code easier to understand and maintain.However, there are use cases for tricks like this. Especially in large-scale mathematical or scientific computing and computer graphics, tricks like these can save your life. An excellent example of this is John Carmack's "magic inverse square root" in Quake 3.
黑客的乐趣这本书有 300 页,除了这样的东西什么也没有。它并不便宜,但它是一本有点无聊的圣经。
The book Hacker's Delight is 300 pages of nothing but stuff like this. It's not cheap but it's a bit-twiddler's bible.