Antti Haapala added the comment:
It seems Eric has done some special casing for strings already in FORMAT_VALUE.
Here are the results from my computer after applying Demur's patch for
concatenating *strings*.
python3.6 -m timeit -s "x = 'a'" -- '"X is %s" % x'
1000000 loops, best of 3: 0.187 usec per loop
python3.6 -m timeit -s "x = 'a'" -- 'f"X is {x}"'
10000000 loops, best of 3: 0.0972 usec per loop
But then as more components are added, it starts to slow down rapidly:
python3.6 -m timeit -s "x = 'a'" -- 'f"X is {x}a"'
1000000 loops, best of 3: 0.191 usec per loop
python3.6 -m timeit -s "x = 'a'" -- '"X is %sa" % x'
1000000 loops, best of 3: 0.216 usec per loop
Of course there is also the matter of string conversion vs "look up __format__":
python3.6 -m timeit -s "x = 1" -- 'f"X is {x}"'
1000000 loops, best of 3: 0.349 usec per loop
python3.6 -m timeit -s "x = 1" -- 'f"X is {x!s}"'
10000000 loops, best of 3: 0.168 usec per loop
For FORMAT_VALUE opcode already has a special case for the latter.
However I'd too say that switch/case for the some fastest builtin types in
`PyObject_Format` as Eric has intended to do with Unicode in PyObject_Format
(str, int, float), as those are commonly used to build strings such as text
templates, text-like protocols like emails, HTTP protocol headers and such.
For others the speed-up wouldn't really matter either way: either
PyObject_Format would fall back to object.__format__ (for example functions) -
no one really cares about efficiency when doing such debug dumps - if you
cared, you'd not do them at all; or they'd have complex representations (say,
lists, dictionaries) - and their use again would mostly be that of debug dumps;
or they'd have `__format__` written in Python and that'd be dwarfed by anything
so far.
----------
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue27078>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com