New submission from Serhiy Storchaka:

Proposed patch makes string.Template compiling template to formatted string 
literal. Since for now using formatted string literals is the fastest way of 
formatting strings, this significantly speeds up Template substitution.

$ ./python -m perf timeit -s 'from string import Template; s = Template("$who 
likes $what")' -- 's.substitute(who="tim", what="ham")'

Unpatched:  Median +- std dev: 46.1 us +- 4.2 us
Patched:    Median +- std dev: 11.1 us +- 0.5 us

The drawback is that compiling template adds high overhead.

$ ./python -m perf timeit -s 'from string import Template' -- 's = 
Template("$who likes $what"); s.substitute(who="tim", what="ham")'

Unpatched:  Median +- std dev: 51.7 us +- 1.5 us
Patched:    Median +- std dev: 672 us +- 38 us

The benefit of using compiled templates is achieved only if make at least 20 
substitutions with the same template.

Third-party template engines can use the same approach in Python 3.6+.

----------
components: Library (Lib)
messages: 277690
nosy: eric.smith, georg.brandl, serhiy.storchaka
priority: low
severity: normal
status: open
title: Accelerate string.Template by using formatted string literals
type: performance
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue28309>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to