为&放大器;检查奇数时超过%的速度? [英] Is & faster than % when checking for odd numbers?

查看:116
本文介绍了为&放大器;检查奇数时超过%的速度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要检查奇数和偶数,是最低位检查比使用模更有效率?

 >>>高清ISODD(NUM):
        返回NUM和放大器; 1和真或假>>> ISODD(10)

>>> ISODD(9)
真正


解决方案

没错。在标准库的 timeit 模块是你这些东西怎么查。例如:

  AmAir:stko aleax $蟒蛇-mtimeit -s'def ISODD(X):X&放大器; 1''ISODD(9)'
百万循环,最好的3:每循环0.446微秒
AmAir:stko aleax $蟒蛇-mtimeit -s'def ISODD(X):X&放大器; 1''ISODD(10)'
百万循环,最好的3:每循环0.443微秒
AmAir:stko aleax $蟒蛇-mtimeit -s'def ISODD(X):X%2'ISODD(10)'
百万循环,最好的3:每循环0.453微秒
AmAir:stko aleax $蟒蛇-mtimeit -s'def ISODD(X):X%2'ISODD(9)'
百万循环,最好的3:每循环0.461微秒

如你所见,我(首日== ==老;-)缓慢的Macbook Air,在&安培; 解决方案是可重复7和18纳秒之间实现更快比解决方案。

timeit 不仅告诉你什么是速度更快,但多少(只运行测试几次),这通常表明它是如何苏premely不重要。为(你的真正的关心10纳秒'的区别,当调用函数的开销大约是400 - ?!)...

说服程序员,微型的优化本质上是无关紧要的已被证明是一个不可能完成的任务 - 尽管它已有35年了克努特的wrote


  

我们应该忘记小
  效率,讲的约97%
  时间:premature优化是
  万恶之源。


这是他解释说是从霍尔的更老的语句引用。我想每个人都完全相信,他们的情况落在剩下的3%!

因此​​,而不是无休止地重复着没关系,我们把标准Python库模块 timeit ,在(蒂姆·彼得斯特别值得有荣誉)相当轻松测量这些微观基准测试,从而让至少的部分的程序员说服自己,嗯,这箱子秋季97%组 - !)

To check for odd and even integer, is the lowest bit checking more efficient than using the modulo?

>>> def isodd(num):
        return num & 1 and True or False

>>> isodd(10)
False
>>> isodd(9)
True

解决方案

Yep. The timeit module in the standard library is how you check on those things. E.g:

AmAir:stko aleax$ python -mtimeit -s'def isodd(x): x & 1' 'isodd(9)'
1000000 loops, best of 3: 0.446 usec per loop
AmAir:stko aleax$ python -mtimeit -s'def isodd(x): x & 1' 'isodd(10)'
1000000 loops, best of 3: 0.443 usec per loop
AmAir:stko aleax$ python -mtimeit -s'def isodd(x): x % 2' 'isodd(10)'
1000000 loops, best of 3: 0.453 usec per loop
AmAir:stko aleax$ python -mtimeit -s'def isodd(x): x % 2' 'isodd(9)'
1000000 loops, best of 3: 0.461 usec per loop

As you see, on my (first-day==old==slow;-) Macbook Air, the & solution is repeatably between 7 and 18 nanoseconds faster than the % solution.

timeit not only tells you what's faster, but by how much (just run the tests a few times), which usually shows how supremely UNimportant it is (do you really care about 10 nanoseconds' difference, when the overhead of calling the function is around 400?!-)...

Convincing programmers that micro-optimizations are essentially irrelevant has proven to be an impossible task -- even though it's been 35 years (over which computers have gotten orders of magnitude faster!) since Knuth wrote

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

which as he explained is a quote from an even older statement from Hoare. I guess everybody's totally convinced that THEIR case falls in the remaining 3%!

So instead of endlessly repeating "it doesn't matter", we (Tim Peters in particular deserves the honors there) put in the standard Python library module timeit, that makes it trivially easy to measure such micro-benchmarks and thereby lets at least some programmers convince themselves that, hmmm, this case DOES fall in the 97% group!-)

这篇关于为&放大器;检查奇数时超过%的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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