问题
Note: It's true that this question has been answered for many other languages. However, I could not find an answer for Python, so do not mark as duplicate.
Is there a difference in performance between the if-else statement and the ternary operator in Python?
回答1:
I doubt there is a performance difference. They compile to equivalent sequences of bytecodes:
>>> def f():
... return a if b else c
...
>>> dis.dis(f)
2 0 LOAD_GLOBAL 0 (b)
2 POP_JUMP_IF_FALSE 8
4 LOAD_GLOBAL 1 (a)
6 RETURN_VALUE
>> 8 LOAD_GLOBAL 2 (c)
10 RETURN_VALUE
>>> def g():
... if b:
... return a
... else:
... return c
...
>>> dis.dis(g)
2 0 LOAD_GLOBAL 0 (b)
2 POP_JUMP_IF_FALSE 8
3 4 LOAD_GLOBAL 1 (a)
6 RETURN_VALUE
5 >> 8 LOAD_GLOBAL 2 (c)
10 RETURN_VALUE
12 LOAD_CONST 0 (None)
14 RETURN_VALUE
As with most performance questions, the answer is to measure.
回答2:
From some testing on Python 3.6.1, it seems like the full statement is faster.
My Results:
>>> timeit.timeit("""3 if True else 8""")
0.012174860001323395
>>> timeit.timeit("""3 if False else 8""")
0.019092951000857283
>>> timeit.timeit("""if True:
... 3
... else:
... 8""")
0.009110345999943092
>>> timeit.timeit("""if False:
... 3
... else:
... 8""")
0.00877297099941643
回答3:
Here're my tests in IPython 7.2.0 (which has %timeit
, a built-in timing method that makes it extremely easy to measure executions. By default it makes 7 runs and 100 million loops each, so the results should usually be valid) used by PyCharm 2018.3.4 Community Edition x64, running CPython 3.7.2 x64. The OS is Window$ 10.0.17134 Enterprise x64:
##The 1st 2 are without quoting the statement to see how it affects the test.
%timeit 3 if True else 8
14.7 ns ± 0.319 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
%timeit 3 if False else 8
18.1 ns ± 0.211 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
##----------------------------------------------------------------------------
%timeit 'if True:\n 3\nelse:\n 8'
8.67 ns ± 0.314 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
%timeit 'if False:\n 3\nelse:\n 8'
8.4 ns ± 0.0598 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
##----------------------------------------------------------------------------
##The last 2 are with quoting the statement.
%timeit '3 if True else 8'
8.73 ns ± 0.256 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
%timeit '3 if False else 8'
9.37 ns ± 0.215 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
I think the numbers speak for the statements. A shame really, cuz I love the ternary op.
However, the ternary op's still absolutely useful, especially in the cases where covering all the possibilities in function call params would create a massive repeating of code, which I absolutely hate.
来源:https://stackoverflow.com/questions/44599860/performance-of-ternary-operator-vs-if-else-statement