Created
May 2, 2013 06:08
-
-
Save diezguerra/5500429 to your computer and use it in GitHub Desktop.
Dict initialization optimization
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
""" | |
Result: | |
Using original function 1000 loops, best of 3: 200 us per loop | |
Using itertools.chain 10000 loops, best of 3: 130 us per loop | |
Using joined sets 10000 loops, best of 3: 142 us per loop | |
Using double the tags and modulo 1000 loops, best of 3: 184 us per loop | |
""" | |
# -*- coding: utf-8 -*- | |
# <nbformat>3.0</nbformat> | |
# <codecell> | |
def original_function(): | |
CONVERSION_LEVELS = 100 | |
conversion_queries_map = { | |
'conversions{:02d}'.format(index): 'conversion{:02d}_query'.format(index) | |
for index in range(CONVERSION_LEVELS)} | |
adg_res_map = {} | |
conversion_queries_map_sum = \ | |
dict([(param, 0) for param, query in \ | |
conversion_queries_map.iteritems()]) | |
conversion_queries_map_sum.update( | |
dict([(("%s_%s" % ('unmatched', param)), 0) \ | |
for param, query in conversion_queries_map.iteritems()])) | |
# <codecell> | |
def using_chain(): | |
from itertools import chain | |
CONVERSION_LEVELS = 100 | |
tags = {'conversions{:02d}'.format(index) | |
for index in range(CONVERSION_LEVELS)} | |
final_dict = {name: 0 for name in | |
chain(tags, ['unmatched_{}'.format(tag) for tag in tags])} | |
# <codecell> | |
def joining_sets(): | |
CONVERSION_LEVELS = 100 | |
simple_tags = {'conversions{:02d}'.format(index) | |
for index in range(CONVERSION_LEVELS)} | |
unmatched_tags = {'unmatched_conversions{:02d}'.format(index) | |
for index in range(CONVERSION_LEVELS)} | |
final_dict = {name: 0 for name in (simple_tags or unmatched_tags)} | |
# <codecell> | |
def duplication_and_indices(): | |
from itertools import chain | |
CONVERSION_LEVELS = 100 | |
tags = ['conversions{:02d}'.format(index % (CONVERSION_LEVELS / 2)) | |
for index in range(CONVERSION_LEVELS)] | |
final_dict = {'{}{}'.format( | |
'unmatched_'[:11 * int(index >= CONVERSION_LEVELS / 2)], | |
name | |
): 0 for index, name in enumerate(tags)} | |
# <codecell> | |
assert original_function() == using_chain() == \ | |
joining_sets() == duplication_and_indices() | |
print "Using original function", | |
%timeit original_function() | |
print "Using itertools.chain", | |
%timeit using_chain() | |
print "Using joined sets", | |
%timeit joining_sets() | |
print "Using double the tags and modulo", | |
%timeit duplication_and_indices() | |
# <codecell> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment