[New-bugs-announce] [issue28307] Accelerate 'string' % (value, ...) by using formatted string literals
Serhiy Storchaka
report at bugs.python.org
Thu Sep 29 04:49:56 EDT 2016
New submission from Serhiy Storchaka:
For now using formatted string literals (PEP498) is the fastest way of formatting strings.
$ ./python -m perf timeit -s 'k = "foo"; v = "bar"' -- '"%s = %r" % (k, v)'
Median +- std dev: 2.27 us +- 0.20 us
$ ./python -m perf timeit -s 'k = "foo"; v = "bar"' -- 'f"{k!s} = {v!r}"'
Median +- std dev: 1.09 us +- 0.08 us
The compiler could translate C-style formatting with literal format string to the equivalent formatted string literal. The code '%s = %r' % (k, v) could be translated to
t1 = k; t2 = v; f'{t1!r} = {t2!s}'; del t1, t2
or even simpler if k and v are initialized local variables.
$ ./python -m perf timeit -s 'k = "foo"; v = "bar"' -- 't1 = k; t2 = v; f"{t1!s} = {t2!r}"; del t1, t2'
Median +- std dev: 1.22 us +- 0.05 us
This is not easy issue and needs first implementing the AST optimizer.
----------
components: Interpreter Core
messages: 277688
nosy: eric.smith, serhiy.storchaka
priority: low
severity: normal
status: open
title: Accelerate 'string' % (value, ...) by using formatted string literals
type: performance
versions: Python 3.7
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28307>
_______________________________________
More information about the New-bugs-announce
mailing list