int vs. float in benchmark testing

Peter Hansen peter at
Fri Feb 20 15:53:04 CET 2004

Bart Nessux wrote:
> Would adding .0 to each of the numbers below turn this into a floating
> point test? Seems too easy.
> def cpu_test():
>     import time
>     start = time.time()
>     x = 0                               # 0.0
>     while x < 9999999:                  # 9999999.0
>        x = x + 1                                # 1.0
>        print x
>     print (time.time()-start)/60
> cpu_test()

Uh, yes it would, as far as it goes, but are you sure you're
learning something useful by doing so?  Floats are always slower
than ints, but you really shouldn't be concerned about speed

The way to decide which to use is this:  if you need floating
point because you are doing math that involves fractional values,
then use floating point.  Otherwise use ints.  End of story.
No performance considerations in most code.


More information about the Python-list mailing list