Last active
August 29, 2015 13:56
-
-
Save drslump/9109647 to your computer and use it in GitHub Desktop.
Benchmarking the use of memoize for regular expression transformations
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
#!/usr/bin/env python | |
def memoize(fn): | |
""" Memoization decorator for a function taking one or more arguments. | |
NOTE: Only hashable values can be given as arguments to the function. | |
http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-decorator-in-the-/#c4 | |
""" | |
class memodict(dict): | |
def __getitem__(self, *args): | |
return dict.__getitem__(self, args) | |
def __missing__(self, args): | |
ret = self[args] = fn(*args) | |
return ret | |
return memodict().__getitem__ | |
import re | |
UNDER_TO_CAMEL = re.compile(r'(^|_)(.)') | |
def underscore_to_camel_case(name): | |
return UNDER_TO_CAMEL.sub(lambda x: x.group(2).upper(), name) | |
@memoize | |
def underscore_to_camel_case_memoized(name): | |
return underscore_to_camel_case(name) | |
def convert_case(obj, converter): | |
if isinstance(obj, dict): | |
return dict( | |
(converter(k), convert_case(v, converter)) | |
for k, v in obj.iteritems() | |
) | |
elif isinstance(obj, list): | |
return map(lambda x: convert_case(x, converter), obj) | |
else: | |
return obj | |
ITERATIONS = 500 | |
import urllib2, json | |
print("Fetching GitHub's public timeline...") | |
req = urllib2.Request('https://github.com/timeline', None, dict(accept='application/json')) | |
resp = urllib2.urlopen(req) | |
content = json.loads(resp.read()) | |
import timeit | |
print("Benchmarking for {0} iterations".format(ITERATIONS)) | |
r1 = timeit.timeit(lambda: convert_case(content, underscore_to_camel_case), number=ITERATIONS) | |
r2 = timeit.timeit(lambda: convert_case(content, underscore_to_camel_case_memoized), number=ITERATIONS) | |
print 'Normal/Memoized : {0}s / {1}s -- {2}%'.format(r1, r2, int(r1 * 100 / r2)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment