Created
April 12, 2012 17:54
-
-
Save methane/2369648 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$d = file_get_contents('index.html'); | |
//$d = htmlspecialchars($d); | |
$d = strtr($d, array('&'=>'&', '<'=>'<', '>'=>'>')); | |
for ($i =0; $i < 10000; ++$i) { | |
strtr($d, array('&'=>'&', '<'=>'<', '>'=>'>')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def unescape_replace(s): | |
return s.replace('<', '<').replace('>', '>').replace('&', '&') | |
def unescape_sub(s, | |
_sub=re.compile('<|>|&').sub, | |
_d = {"&": "&", ">": ">", "<": "<"} | |
): | |
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 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment