[issue18020] html.escape 10x slower than cgi.escape

Florent Xicluna report at bugs.python.org
Mon May 20 10:21:38 CEST 2013


New submission from Florent Xicluna:

I noticed the convenient ``html.escape`` in Python 3.2 and ``cgi.escape`` is marked as deprecated.


However, the former is an order of magnitude slower than the latter.

$ python3 --version
Python 3.3.2


With html.escape:

$ python3 -m timeit -s "from html import escape as html; from cgi import escape; s = repr(copyright)" "h = html(s)"
10000 loops, best of 3: 48.7 usec per loop
$ python3 -m timeit -s "from html import escape as html; from cgi import escape; s = repr(copyright) * 19" "h = html(s)"
1000 loops, best of 3: 898 usec per loop

With cgi.escape:

$ python3 -m timeit -s "from html import escape as html; from cgi import escape; s = repr(copyright)" "h = escape(s)"
100000 loops, best of 3: 7.42 usec per loop
$ python3 -m timeit -s "from html import escape as html; from cgi import escape; s = repr(copyright) * 19" "h = escape(s)"
10000 loops, best of 3: 21.5 usec per loop


Since this kind of function is called frequently in template engines, it makes a difference.
Of course C replacements are available on PyPI: MarkupSafe or Webext

But it would be nice to restore the performance of cgi.escape with a pragmatic `.replace(` approach.

----------
components: Library (Lib)
messages: 189641
nosy: ezio.melotti, flox, orsenthil
priority: normal
severity: normal
status: open
title: html.escape 10x slower than cgi.escape
type: performance
versions: Python 3.2, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18020>
_______________________________________


More information about the Python-bugs-list mailing list