Piper
Piper

Reputation: 1275

Is it less computationally intensive to use 7.0 or float(7) in Python?

I'm curious which form is more efficient, is correct style, etc. I feel like the ".0" approach is much quicker; I'm not sure why the "float" approach is equally appreciated (if it is).

Upvotes: 9

Views: 1392

Answers (7)

Julian
Julian

Reputation: 2654

7.0 should be faster. float(7) creates an integer, then call the float() function to convert the integer to a float, so calling float(7) implicates function call overhead as well as any error checking the float() function might do.

For most practical purposes, of course, the difference in speed is unlikely to matter much (unless you're in a deep loop where your code is being called hundreds of millions of times), but there's something inelegant about invoking a conversion function when the interpreter has a built in syntax for constructing floats.

Use float() when you have something that isn't a float (like a string or an integer) that you want to convert.

Upvotes: 0

Robbie Rosati
Robbie Rosati

Reputation: 1205

The following are all equivalent in Python:

>>> 7. == 7.0 == float(7) == float("7")
True

I would avoid using float(7) or float("7") when you are hard-coding the value, as the Python interpreter must first cast the value to an integer or a string, and then convert it to floating-point.

To avoid that overhead, use 7. or 7.0 to give Python a float literal.

Of course, float() should still be used to convert other data types to a float.

Upvotes: 6

Michael Hoffman
Michael Hoffman

Reputation: 34354

Using float(7) adds some unnecessary overhead—Python has to find the float function in globals() and call it. Using 7.0 does all the necessary conversions at compile-time instead of run-time. You can see this using the Python bytecode disassembler.

>>> import dis
>>> def f(): return 7.0
... 
>>> def g(): return float(7)
... 
>>> dis.dis(f)
  1           0 LOAD_CONST               1 (7.0)
              3 RETURN_VALUE        
>>> dis.dis(g)
  1           0 LOAD_GLOBAL              0 (float)
              3 LOAD_CONST               1 (7)
              6 CALL_FUNCTION            1
              9 RETURN_VALUE        

Upvotes: 9

CraigTeegarden
CraigTeegarden

Reputation: 8251

Using float(0) is much more explicit when re-reading your code at a later date, causing less confusion later if you accidentally drop the ".0".

Upvotes: 0

Hassek
Hassek

Reputation: 8995

use the 7.0 approach, the float(7) approach is used to transform integer or string types to floats so it's a different use, for example:

a = 7
b = "7"
print float(a)
7.0
print float(b)
7.0

Upvotes: 9

Simeon Visser
Simeon Visser

Reputation: 122476

Float literals can be written as 7.0, that is fine as they are automatically of type float.

If you intend to convert an integer or string to float then the float() function is appropriate but that function does not need to be called to write a float literal.

Upvotes: 1

Rob Wagner
Rob Wagner

Reputation: 4421

7.0 is "better", no need for the cast to float, it will do it automatically.

float() is best saved for casting a non-float to a float.

Upvotes: 0

Related Questions