public
Created

  • Download Gist
unescape.php
PHP
1 2 3 4 5 6 7
<?php
$d = file_get_contents('index.html');
//$d = htmlspecialchars($d);
$d = strtr($d, array('&'=>'&amp;', '<'=>'&lt;', '>'=>'&gt;'));
for ($i =0; $i < 10000; ++$i) {
strtr($d, array('&amp;'=>'&', '&lt;'=>'<', '&gt;'=>'>'));
}
unescape.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import re
 
def unescape_replace(s):
return s.replace('&lt;', '<').replace('&gt;', '>').replace('&amp;', '&')
 
def unescape_sub(s,
_sub=re.compile('&lt;|&gt;|&amp;').sub,
_d = {"&amp;": "&", "&gt;": ">", "&lt;": "<"}
):
return _sub(lambda m: _d[m.group()], s)
 
 
import cgi
d = cgi.escape(open('index.html').read())
assert unescape_replace(d) == unescape_sub(d)
 
N=10000
import timeit
print "replace", timeit.timeit(lambda: unescape_replace(d), number=N)
print "re.sub", timeit.timeit(lambda: unescape_sub(d), number=N)
 
"""
$ wget http://www.python.org/
$ python unescape.py
replace 2.49956488609
re.sub 7.33878493309
"""

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.