improve iteritems performance or a better way to loop through python dictionary -
it takes approx. 190s complete loop below on laptop intel i5, 8gb, windows 7.
count = 0 l in xrange(num_room): k in xrange(num_slot): m in xrange(num_activity): count = count+1 dk, [om, ol, ok] in xdict.iteritems(): if dk == (m,l,k): #
i did trace , found out total loop 13960 x 19040 ~ 2bils.
len(self.xdict): 13960 count: 19040
xdict looks (an example):
xdict = {(19, 4, 118): [19, 4, 4], (4, 12, 25): [4, 12, 7], (15, 19, 121): [15, 19, 21], (23, 5, 219): [23, 5, 9], (13, 5, 19): [13, 5, 1]}
i trying value of key in dictionary. there anyway improve performance?
sorry, think know happen.
count = 0 l in xrange(num_room): k in xrange(num_slot): m in xrange(num_activity): count = count+1 val = xdict.get((m,l,k)) if val != none: [om, ol, ok] = val #
now takes 1s.
don't loop on items just find 1 key. use membership test instead:
if (m, l, k) in xdict: om, ol, ok = xdict[m, l, k]
this removes need loop on 13960 items each of 19040 iterations.
you haven't told use trying achieve here, if using m
, l
, k
else too, consider looping on keys instead; way you'd reduce loop on 13960 items instead of 19040 iterations.
Comments
Post a Comment