import collections import itertools as it import sys import IPython.core.ipapi ip = IPython.core.ipapi.get() class Dict(collections.defaultdict): def __init__(self): super(Dict, self).__init__(Dict) def create_dict_1(): global o o = Dict() for a,b,c,d,e in it.combinations('abcdefghij',5): o[a][b][c][d][e] = True return o def access_dict1_1(): for a,b,c,d,e in it.combinations('abcdefghij',5): x = e in o[a][b][c][d] def access_dict1_2(): for a,b,c,d,e in it.combinations('abcdefghijk',5): x = e in o[a][b][c][d] def create_dict_2(): global o o = dict() for t in it.combinations('abcdefghij',5): o['.'.join(t)] = True return o def access_dict2_1(): for t in it.combinations('abcdefghij',5): x = '.'.join(t) in o def access_dict2_2(): for t in it.combinations('abcdefghijk',5): x = '.'.join(t) in o def create_set(): global o o = set() for t in it.combinations('abcdefghij',5): o.add('.'.join(t)) return o def access_set_1(): for t in it.combinations('abcdefghij',5): x = '.'.join(t) in o def access_set_2(): for t in it.combinations('abcdefghijk',5): x = '.'.join(t) in o print 'create nested dicts' d1 = create_dict_1() ip.magic('timeit create_dict_1()') print 'size', sys.getsizeof(d1) print 'access 1', ip.magic('timeit access_dict1_1()') print 'access 2', ip.magic('timeit access_dict1_2()') print print 'create dict with path keys (i.e. creature.Fire Hound.magic...)' d2 = create_dict_2() ip.magic('timeit create_dict_2()') print 'size', sys.getsizeof(d2) print 'access 1', ip.magic('timeit access_dict2_1()') print 'access 2', ip.magic('timeit access_dict2_2()') print print 'create set' s = create_set() ip.magic('timeit create_set()') print 'size', sys.getsizeof(s) print 'access 1', ip.magic('timeit access_set_1()') print 'access 2', ip.magic('timeit access_set_2()')