[Baypiggies] Notes on defaultdict
Chris Clark
Chris.Clark at ingres.com
Fri Aug 27 21:01:59 CEST 2010
Great discussion, thanks for kicking this off Glen!
As Brian pointed out syntax/simplicity is the reason for default dict,
performance is not in the running. I actually almost never use the
defaultdict, part of the reason is that I'm often on a pre 2.5
interpreter. You can use the regular dict and still keep the code fairly
clean using get(), but it isn't as nice as defaultdict.
"""Performance figures/output from my desktop:
12.9385441449 def_dict
6.60369553361 reg_dict
"""
import timeit
def def_dict():
from collections import defaultdict # NOTE needs python 2.5+
input = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4),
('red', 1)]
output = defaultdict(list)
for color, value in input:
output[color].append(value)
def reg_dict():
input = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4),
('red', 1)]
output2 = {}
for color, value in input:
tmp_entry = output2.get(color, [])
tmp_entry.append(value)
output2[color] = tmp_entry
if __name__ == '__main__':
names = ['def_dict', 'reg_dict']
m = 5
m = 1000000
for name in names:
t = timeit.Timer('%s()' % name, 'from __main__ import %s' % name)
tmp_time = t.timeit(m)
print tmp_time, name
NOTE I followed Brian's approach of timing the import from collections,
and setting up the source list.
Chris
Glen Jarvis wrote:
>
> Great notes -- thanks for posting them, I'm glad to see them as I
> wasn't able to be at the meeting.
>
> Did you mention the __missing__ magic method, in Python 2.5 and up,
> which can be used for defaultdict-like (and other fun) behavior?
>
>
> I didn't... I didn't know about it, actually... In the future, I
> obviously need to send this *first* to BayPIGgies a few weeks ahead to
> get all of this valuable feedback up front. There are still layers to
> learn/understand here. We have *plenty* of choices to handle this one
> itty bitty situation...
>
> Cheers,
>
>
> Glen
> --
> Whatever you can do or imagine, begin it;
> boldness has beauty, magic, and power in it.
>
> -- Goethe
> ------------------------------------------------------------------------
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
More information about the Baypiggies
mailing list