Python 3 is precomputing the value of 2 ** 0.5
at compile time, since both operands are known at that time. The value of sqrt
, however, is not known at compile time, so the computation necessarily occurs at run time.
You aren't timing how long it takes to compute 2 ** 0.5
, but just the time it takes to load a constant.
A fairer comparison would be
$ python3 -m timeit -s "from math import sqrt" "sqrt(2)"
5000000 loops, best of 5: 50.7 nsec per loop
$ python3 -m timeit -s "x = 2" "x**0.5"
5000000 loops, best of 5: 56.7 nsec per loop
I'm not sure if there is a way to show unoptimized byte code. Python starts by parsing source code into an abstract syntax tree (AST):
>>> ast.dump(ast.parse("2**0.5"))
'Module(body=[Expr(value=BinOp(left=Num(n=2), op=Pow(), right=Num(n=0.5)))])'
Update: This particular optimization is now applied directly to the abstract syntax tree, so the byte code is generated directly from something like
Module(body=Num(n= 1.4142135623730951))
The ast
module doesn't appear to apply the optimization.
The compiler takes the AST and generates unoptimized byte code; in this case, I believe it would look (based on the output of dis.dis("2**x")
and dis.dis("x**0.5")
) like
LOAD_CONST 0 (2)
LOAD_CONST 1 (0.5)
BINARY_POWER
RETURN_VALUE
The raw byte code is then subject to modification by the peephole optimzizer, which can reduce these 4 instructions to 2, as shown by the dis
module.
The compiler then generates byte code from the AST.
>>> dis.dis("2**0.5")
1 0 LOAD_CONST 0 (1.4142135623730951)
2 RETURN_VALUE
[While the following paragraph was originally written with the idea of optimizing byte code in mind, the reasoning applies to optimizing the AST as well.]
Since nothing at runtime affects how the two LOAD_CONST
and following BINARY_POWER
instruction are evaluated (for example, there are no name lookups), the peephole optimizer can take this sequence of byte codes, perform the computation of 2**0.5
itself, and replace the first three instructions with a single LOAD_CONST
instruction that loads the result immediately.
import
, and the second doesn't.sqrt
has already been looked up in the setup.