Which is More Efficient?

Fredrik Lundh fredrik at pythonware.com
Thu May 18 09:39:39 EDT 2006


Dustan wrote:

> I have a program that uses up a lot of CPU and want to make it is
> efficient as possible with what I have to work with it. So which of the
> following would be more efficient, knowing that l is a list and size is
> a number?
> 
> l=l[:size]
> del l[size:]

since you have the program, it shouldn't that hard to test the
two alternatives, should it ?

(in theory, del should be faster in most cases, since it avoids
creating another object.  but the only way to tell for sure is
to try it out).

> If it makes a difference, everything in the list is mutable.

the only difference between mutable and immutable objects in Python
is that mutable objects have methods that let you modify the object
contents, while immutable objects don't have such methods.

</F>




More information about the Python-list mailing list