Author flox
Recipients ezio.melotti, flox, orsenthil
Date 2013-05-20.08:21:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1369038098.34.0.795942514317.issue18020@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2013-05-20 08:21:38floxsetrecipients: + flox, orsenthil, ezio.melotti
2013-05-20 08:21:38floxsetmessageid: <1369038098.34.0.795942514317.issue18020@psf.upfronthosting.co.za>
2013-05-20 08:21:38floxlinkissue18020 messages
2013-05-20 08:21:37floxcreate