newbie questions

John Machin sjmachin at lexicon.net
Sun Dec 12 14:55:09 EST 2004


Fredrik Lundh wrote:
> John Machin wrote:
>
> >> Of course, in this simple case, I wouldn't be likely to write the
clear
> >> function since the inline code is simpler and has less overhead:
> >>
> >> def main()
> >>      var1 = []
> >>      var1.append('a')
> >>      var1[:] = []
> >
> > Even less overhead: del var1[:]
>
> even less overhead:
>
>     var1 = []

Firstly, your replacement is not functionally equivalent. Secondly, it
appears *NOT* to have less overhead:

>python -m timeit "a=range(100); del a[:]"
100000 loops, best of 3: 3.56 usec per loop
>python -m timeit "a=range(100); a=[]"
100000 loops, best of 3: 3.95 usec per loop
>python -m timeit "a=range(1000); del a[:]"
10000 loops, best of 3: 25.3 usec per loop
>python -m timeit "a=range(1000); a=[]"
10000 loops, best of 3: 33.1 usec per loop
[Python 2.4; Windows 2000; Athlon 1.4Ghz chip]
>
> (if you rely on having multiple references to the same list, instead
of referring
> to the list by name, you may want to reconsider the design)
>

Indeed. The OP's insistence on emptying the container instead of
trashing it along with its contents -- which is my interpretation of
"it doesn't emty my var (i don't want to destroy the var, just make it
like if it just have been created" -- was intriguing but not explained
...




More information about the Python-list mailing list