I stumbled across an interesting optimization question while writing some mathematical derivative functions for a neural network library. Turns out that the expression a / (b*c)
takes longer to compute than a / b / c
for large values (see timeit
below). But since the two expressions are equal:
- shouldn't Python be optimized both in the same way on the down-low?
- is there a case for
a / (b*c)
, given that it seems to be slower? - or am I missing something, and the two are not always equal?
Thanks in advance :)
In [2]: timeit.timeit('1293579283509136012369019234623462346423623462346342635610 / (52346234623632464236234624362436234612830128521357*32189512234623462637501237)')
Out[2]: 0.2646541080002862
In [3]: timeit.timeit('1293579283509136012369019234623462346423623462346342635610 / 52346234623632464236234624362436234612830128521357 / 32189512234623462637501237')
Out[3]: 0.008390166000026511