Luiz Eduardo de Christo Luiz Eduardo de Christo - 1 month ago 8
Python Question

Python integer division operator vs math.floor

Is there any benefit in performance using integer division operator over math.floor?

7 // 2


over

math.floor(7/2)

Answer

Integer division is much faster than a math.floor function call:

>>> import timeit
>>> timeit.timeit('7//2')
0.024671780910702337
>>> timeit.timeit('floor(7/2)', setup='from math import floor')
0.27053647879827736
>>> timeit.timeit('math.floor(7/2)', setup='import math')
0.3131167508719699