En Fri, 20 Apr 2007 14:28:00 -0300, Larry Bates <[EMAIL PROTECTED]> escribió:
> Bill Jackson wrote: >> What is the benefit of clearing a dictionary, when you can just reassign >> it as empty? > > If you have objects that point to the dictionary (something like a cache) > then you want to clear the existing dictionary instead of just assigning > it to empty. If nothing points to it, assigning it to empty is fast and > you can let garbage collection do the rest. For an actual comparision, see Alex Martelli posts a few days ago: http://mail.python.org/pipermail/python-list/2007-March/433027.html >>>>> a = {1:2,3:4} >>>>> b = {1:2:4:3} >>>>> a.clear() >>>>> a.update(b) >> >>>>> a = {1:2,3:4} >>>>> b = {1:2,4:3} >>>>> for key in b: >> ... a[key] = b[key] >> > Syntax error in the first example but if you fix that the first two are > equivalent (but I would suspect that the second would be faster for large > dictionaries). It's the other way; the first method contains a single Python function call and most of the work is done in C code; the second does the iteration in Python code and is about 4x slower. > python -m timeit -s "b=dict.fromkeys(range(10000));a={}" "a.update(b)" 100 loops, best of 3: 10.2 msec per loop > python -m timeit -s "b=dict.fromkeys(range(10000));a={}" "for key in b: > a[key]=b[key]" 10 loops, best of 3: 39.6 msec per loop -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list