#============================================================================== # Speed test for http://bugs.python.org/issue1285086 # # Updated to test against the improved urlib.quote in Python >= 2.5. #============================================================================== import timeit _std_quote_setup = "from urllib import quote\n" _new_quote_setup = """\ #: list of characters that are always safe in URLs. always_safe = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz' '0123456789' '_.-') + '/' safe_map = dict((c, c) for c in always_safe) for i in xrange(0x80): c = chr(i) if c not in safe_map: safe_map[c] = '%%%02X' % i safe_map.update((chr(i), '%%%02X' % i) for i in xrange(0x80, 0x100)) """ # Variant 1: using regexp and {}.get(...) _msg104639_quote_setup = """\ import re _must_quote = re.compile(r'[^%s]' % re.escape(always_safe)) def new_quote(to_quote): if not to_quote: return to_quote if not _must_quote.search(to_quote): return to_quote return ''.join(map(safe_map.get, to_quote)) """ # Variant 2: using regexp and caching {}.__getitem__ attribute lookup _msg105486_quote_setup = """\ import re is_unsafe = re.compile(r'[^%s]' % re.escape(always_safe)).search safe_get = safe_map.__getitem__ def new_quote(s): if not s: return s return ''.join(map(safe_get, s)) if is_unsafe(s) else s """ # Variant 3: using str.translate(...) and caching attribute lookup _msg105513_quote_setup = """\ strans = ''.join('% '[chr(i) in always_safe] for i in xrange(0x100)) safe_get = safe_map.__getitem__ def new_quote(s): if not s or '%' not in s.translate(strans): return s return ''.join(map(safe_get, s)) """ # Variant 4: using rstrip(...) and caching attribute lookup _msg105523_quote_setup = """\ safe_get = safe_map.__getitem__ def new_quote(s): if not s or not s.rstrip(always_safe): return s return ''.join(map(safe_get, s)) """ # Select a variant #_new_quote_setup += _msg104639_quote_setup #_new_quote_setup += _msg105486_quote_setup #_new_quote_setup += _msg105513_quote_setup _new_quote_setup += _msg105523_quote_setup TO_QUOTE = [ '', 'a', 'ABCDEFGHIJK', 'abcdefghijklmnopqrstuvwxyz', 'abc/def/ghi/jkl/mno', # 'abc/def/ghi/jkl/mno' * 3, 'abc!@#$%^&*()[]{}\|\;\'";:.>, 38: to_quote = to_quote[:35] + '...' print '%-40s : %11.3f %11.3f %9.f %%' % ('[%s]' % to_quote, stdlib, mine, (stdlib - mine) * 100/stdlib)