Message 189641 - Python tracker (original) (raw)

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.