Which is More Efficient?

Dustan DustanGroups at gmail.com
Thu May 18 18:25:55 EDT 2006


Fredrik Lundh wrote:
> 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.

And it can be referenced by different variables. What I was saying was
that the contents weren't being copied over; it was only the list that
was being copied in the first statement.

> 
> </F>




More information about the Python-list mailing list