Created
April 23, 2012 10:07
-
-
Save dobrokot/2470067 to your computer and use it in GitHub Desktop.
compare set contains element on strings of ~tens symbols
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 f1(c): | |
return c in ''' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"\\'()*-.:<>[]^`{|}~''' | |
ALLOWED = re.compile('''[ a-zA-Z0-9!"'()*.:<>^`{|}~\\\[\]-]''') | |
def f2(c): | |
return bool(re.match(ALLOWED, c)) | |
boolean_table = [bool(re.match(ALLOWED, chr(c))) for c in xrange(0,256)] | |
def f3(c): | |
return boolean_table[ord(c)] | |
char_set = set(''' abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!"\\'()*-.:<>[]^`{|}~''') | |
def f4(c): | |
return c in char_set | |
def f5(c): # estimate function call overhead | |
return True | |
for i in xrange(0, 256): | |
c = chr(i) | |
assert f1(c) == f2(c) == f3(c) == f4(c), [f1(c) , f2(c) , f3(c) , f4(c), c] | |
import timeit | |
for f in 'f1 f2 f3 f4 f5'.split(): | |
all_chars = repr(''.join(map(chr, xrange(256)))) | |
print timeit.Timer('for x in %s: %s(x)' % (all_chars, f), "from __main__ import " + f).repeat(number = 1000) | |
# s = 'http://xn--80a1acny.xn--p1ai/resp_engine.aspx?Path=RP/INDEX/RU/Home/Search&keyword=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82' | |
# s = 'https://www.google.com/#hl=en&sclient=psy-ab&q=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82!&oq=%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82!&aq=f&aqi=g4&aql=&gs_l=hp.3..0l4.10388l11609l1l11785l7l6l0l0l0l0l235l1014l0j5j1l6l0.&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=bb242dc4ab43ba95&biw=1701&bih=1310' | |
# s = 'https://twitter.com/#!/search/%23%D1%85%D1%83%D0%B9' | |
# s = 'http://xn--c1aay4a.xn--p1ai/search?q=%26other_param%3D10%26q%3D30%26%D1%80%D1%83%D1%81%D1%81%D0%BA%D0%B8%D0%B5_%D0%B1%D1%83%D0%BA%D0%B2%D1%8B%28%D0%B4%D0%B0!%29' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment