Last active
December 15, 2015 05:59
-
-
Save georgexsh/5212626 to your computer and use it in GitHub Desktop.
simple benchmark
This file contains hidden or 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 | |
from timeit import timeit | |
def str_replace(s): | |
"""legacy HTML escape for non-unicode mode.""" | |
s = s.replace("&", "&") | |
s = s.replace(">", ">") | |
s = s.replace("<", "<") | |
s = s.replace('"', """) | |
s = s.replace("'", "'") | |
return s | |
xml_escapes = { | |
'&': '&', | |
'>': '>', | |
'<': '<', | |
'"': '"', # also " in html-only | |
"'": ''' # also ' in html-only | |
} | |
LEGACY_HTML_ESCAPE_RE = re.compile(r'([&<"\'>])') | |
def re_sub(string): | |
return LEGACY_HTML_ESCAPE_RE.sub(lambda m: xml_escapes[m.group()], string) | |
s = """ | |
<a href="/georgexsh" title="George Xie"><img class="avatar avatar32" src="https://secure.gravatar.com/avatar/1ae2e5f73c85f5dd836e3f9d1ccdc764?d=https%3A%2F%2Fd3oaxc4q5k2d6q.cloudfront.net%2Fm%2F8779a3c76276%2Fimg%2Fdefault_avatar%2F32%2Fuser_blue.png&s=32" alt="George Xie avatar" /><span>George Xie</span></a> | |
</li> | |
</ol> | |
<script id="no-reviewers-template" type="text/html"> | |
<li>No reviewers</li> | |
</script> | |
</div> | |
</dd> | |
</div> | |
<div class="clearfix reviewers-group"> | |
<dt> | |
Reviewers | |
</dt> | |
<dd class="participants"> | |
<ol class="clearfix"> | |
<li class="empty"></li> | |
</ol> | |
</dd> | |
</div> | |
<div class="clearfix description"> | |
<dt>Description</dt> | |
<dd class="wiki-content"> | |
<p>they are equivalent</p> | |
</dd> | |
</div> | |
</dl> | |
</div> | |
<div class="sidebar"> | |
<ul class="primary"> | |
<li class="created iconfont"> | |
<span class="aui-icon aui-icon-small aui-iconfont-time"></span> | |
Created | |
<time datetime="2013-03-21T04:11:00.407924+00:00" data-title="true">6 hours ago</time> | |
</li> | |
<li class="updated iconfont"> | |
<span class="aui-icon aui-icon-small aui-iconfont-time"></span> | |
Updated | |
<time datetime="2013-03-21T04:47:51.540289+00:00" data-title="true">5 hours ago</time> | |
</li> | |
</ul> | |
<ul class="secondary"> | |
<li class="watch watching"> | |
<a href="/zzzeek/mako/pull-request/4/follow" | |
data-template="pullrequest-watch-template" | |
data-type="pull request" | |
class="follow following" | |
title="Stop watching this pull request"> Stop watching | |
</a> | |
</li> | |
<script id="pullrequest-watch-template" type="text/html"> | |
<a href="/zzzeek/mako/pull-request/4/follow" | |
data-template="pullrequest-watch-template" | |
data-type="pull request" | |
[[#isFollowing]] | |
class="follow following" | |
title="Stop watching this pull request"> Stop watching | |
[[/isFollowing]] | |
[[^isFollowing]] | |
class="follow" | |
title="Watch this pull request"> Watch this pull request | |
[[/isFollowing]] | |
</a> | |
</script> | |
<li class="learn-more iconfont"> | |
<span class="aui-icon aui-icon-small aui-iconfont-help"></span> | |
<a href="https://confluence.atlassian.com/display/BITBUCKET/Working+with+pull+requests" target="_blank"> | |
Learn about pull requests | |
</a> | |
</li> | |
</ul> | |
</div> | |
</header> | |
""" | |
def a(): | |
str_replace(s) | |
print 'a:', timeit('a()', setup='from __main__ import a', number=10000) | |
def b(): | |
re_sub(s) | |
print 'b:', timeit('b()', setup='from __main__ import b', number=10000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment