爲什麼x ** 4.0比Python 3中的x ** 4快? - Why is x**4.0 faster than x**4 in Python 3?

問題:

Why is x**4.0 faster than x**4 ? 爲什麼x**4.0x**4快? I am using CPython 3.5.2. 我正在使用CPython 3.5.2。

$ python -m timeit "for x in range(100):" " x**4.0"
  10000 loops, best of 3: 24.2 usec per loop

$ python -m timeit "for x in range(100):" " x**4"
  10000 loops, best of 3: 30.6 usec per loop

I tried changing the power I raised by to see how it acts, and for example if I raise x to the power of 10 or 16 it's jumping from 30 to 35, but if I'm raising by 10.0 as a float, it's just moving around 24.1~4. 我嘗試改變我所提出的力量,看看它是如何動作的,例如,如果我將x提升到10或16的力量,它會從30跳到35,但如果我將10.0提升爲浮動,它只是移動大約24.1~4。

I guess it has something to do with float conversion and powers of 2 maybe, but I don't really know. 我想它可能與浮點轉換和2的冪有關,但我真的不知道。

I noticed that in both cases powers of 2 are faster, I guess since those calculations are more native/easy for the interpreter/computer. 我注意到在兩種情況下,2的冪都更快,我想因爲這些計算對於解釋器/計算機而言更加原生/容易。 But still, with floats it's almost not moving. 但是,對於浮子,它幾乎沒有移動。 2.0 => 24.1~4 & 128.0 => 24.1~4 but 2 => 29 & 128 => 62 2.0 => 24.1~4 & 128.0 => 24.1~4 但是 2 => 29 & 128 => 62


TigerhawkT3 pointed out that it doesn't happen outside of the loop. TigerhawkT3指出它不會發生在循環之外。 I checked and the situation only occurs (from what I've seen) when the base is getting raised. 我檢查過,當基地升起時,情況纔會發生(從我所看到的情況)。 Any idea about that? 有什麼想法嗎?


解決方案:

參考一: https://en.stackoom.com/question/2riWI
參考二: https://stackoom.com/question/2riWI
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章