Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
$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;'=>'>'));
import re
def unescape_replace(s):
return s.replace('&lt;', '<').replace('&gt;', '>').replace('&amp;', '&')
def unescape_sub(s,
_d = {"&amp;": "&", "&gt;": ">", "&lt;": "<"}
return _sub(lambda m: _d[], s)
import cgi
d = cgi.escape(open('index.html').read())
assert unescape_replace(d) == unescape_sub(d)
import timeit
print "replace", timeit.timeit(lambda: unescape_replace(d), number=N)
print "re.sub", timeit.timeit(lambda: unescape_sub(d), number=N)
$ wget
$ python
replace 2.49956488609
re.sub 7.33878493309
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment