python 編程小技巧

2的n次冪

<< 是最快的執行方法,移位操作符。

In [613]: %timeit 1 << 10
The slowest run took 78.83 times longer than the fastest. This could mean that an intermediate result is being cached.
100000000 loops, best of 3: 19.5 ns per loop

In [614]: %timeit 2 ** 10
100000000 loops, best of 3: 19.7 ns per loop

In [615]: %timeit pow(2, 10)
The slowest run took 6.41 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 601 ns per loop

參考博客

2的(冪)n次方實現方式比較

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章