dictionary.update() enhancement?

Mark J maj64 at hotmail.com
Sat Oct 13 14:31:03 EDT 2001


"Tim Peters" <tim.one at home.com> wrote in message news:<mailman.1002941714.8052.python-list at python.org>...
> [Mark J]
> > The above enhancement ...
> > would be much faster than anything that can currently be implemented
> > within Python (by subclassing dictionary and overriding the update
> > method with hand iteration).
> 
> Unfortunately, it would be slower than hand iteration.  David Bolen went
> into the most detail about that, and he's right:  the overhead of calling
> back into a Python function each time would kill performance.
>
> Compare, e.g.,
> 
>     x = 3
>     for i in xrange(1000000):
>         y = x+x
> 
> to
> 
>     def add(i, j):
>         return i+j
>     x = 3
>     for i in xrange(1000000):
>         y = add(x, x)
> 
> for a direct comparison of doing a thing by hand inline versus calling a
> Python function to do it.

When I ran the above comparison, the extra call to the Python function
performed a bit less than half as fast as the inline function (43%). 
This is about the cost that I was figuring on.

> I should also note that the actual implementation of dict.update does no
> "collision checking" at all;

Ya, I should have kept the collision conditional out of the loop in
withloop() for a fairer test.  Without the collision test, the hand
iteration is still about 10x slower.  When I used other.iteritems()
instead of other.items() with Python2.2a4, I didn't notice a change in
performance until dictsize was over about 3000.  After about
dictsize=5000, there was roughly a 2x speedup using iteritems() over
item().

I'll drop the case.  Nonetheless, I appreciate the discussion...

Mark



More information about the Python-list mailing list