1

Something surprising I just discovered:

>>> def f():
...  float('1')
...
>>> def i():
...  int('1')
...
>>> timeit(f, number=1000000)
0.22292590141296387
>>> timeit(i, number=1000000)
0.4127480983734131

I repeated this many times. float always performs substantially better than int. Why is this happening? What goes on behind the scenes?

martineau
  • 119,623
  • 25
  • 170
  • 301
ewok
  • 20,148
  • 51
  • 149
  • 254
  • Just tested `%timeit float('1')` and `%timeit int('1')` in IPython (3) and got opposite results, `int` somewhat faster than `float`... – jdehesa Mar 21 '18 at 14:37

0 Answers0