[Dan Christensen]
> It's true that this runs at the same speed as the del variants on my
> machine.  That's not too surprising to me, but I still don't
> understand why the del variants are more than 5% faster than the first
> version.

Understanding it involves looking at implementation specific details
such as the overallocation scheme for growing lists and the performance
of your machine's underlying memory allocator.


> Once this is understood, is it something that could be optimized?
> It's pretty common to rebind a variable to a new value, and if
> this could be improved 5%, that would be cool.

Sorry, that's not how the language works.  Something like
"a=range(100000)" means:
* build a NEW list for 100000 elements
* THEN assign it to the variable "a"
* which THEN reduces the ref count to the previous binding for "a"
* and THEN IF the ref count is zero, free the list previously bound to
"a"

In other words, if "a" is already bound to a large list, then the above
assignment necessarily creates a second, non-overlapping list in
memory.

However, if you write "a=None;a=range(100000)", then the original list
gets freed BEFORE the new list is created and the system has a chance
to re-use that large, contiguous block of memory.

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to