[Python-Dev] backticks delenda est
Anthony Baxter
anthony at ekit-inc.com
Mon Dec 1 01:15:46 EST 2003
>>> "Raymond Hettinger" wrote
> Advisory from a micro-performance hawk: Backticks are faster than
> repr()
>
> >>> from timeit import Timer
> >>> min(Timer('`x`', 'x=1').repeat(3))
> 1.4857213496706265
> >>> min(Timer('repr(x)', 'x=1').repeat(3))
> 1.7748914665012876
Presumably because the backtick version doesn't have to look up
'repr', then call it.
>>> def f1(a):
... repr(a)
...
>>>
>>> def f2(a):
... `a`
...
>>> import dis
>>> dis.dis(f1)
2 0 LOAD_GLOBAL 0 (repr)
3 LOAD_FAST 0 (a)
6 CALL_FUNCTION 1
9 POP_TOP
10 LOAD_CONST 0 (None)
13 RETURN_VALUE
>>> dis.dis(f2)
2 0 LOAD_FAST 0 (a)
3 UNARY_CONVERT
4 POP_TOP
5 LOAD_CONST 0 (None)
8 RETURN_VALUE
This is presumably something that a future optimiser could "fix",
once we have the no-shadowing-builtins rule enforced.
I don't think this is a serious enough problem to merit leaving the
backticks in the std lib. I find them annoying, and hard to read.
In general, the backticks are in debugging and error handling code,
so the performance penalty should be negligible.
Anthony
More information about the Python-Dev
mailing list