Creating a List of Empty Lists

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Dec 9 07:06:31 EST 2003


Robin Becker <robin at jessikat.fsnet.co.uk> wrote in 
news:tOjKkIAu$a1$Ewoy at jessikat.fsnet.co.uk:

> Duncan Booth's prompted me to repeat all the nonsense about empty lists
> of a few years ago. I am amazed at the differences between pythons.
> Lambda is now faster than list!!! I would really like to know why
> list([]) is so much slower than list(()). Clearly comprehensions are now
> fast, but still slower than the corresponding map with a lambda. 

May I ask you to repeat your tests but putting all the code inside 
functions, please? I think it is a bit unfair to compare a list 
comprehension with a map/lambda and force the list comprehension to access 
a global variable every time round the loop.

I think if you let the list comprehension use a local variable it should 
comfortably beat the best of your lambdas (at least it does on my system).

By the way, I didn't understand your earlier comment:

> unfortunately all of these tests are slower than 1 clock tick on my
> machine. I believe the may be biassed.

You obviously think something was wrong with my earlier timings, but I 
can't understand from this what the problem was; timeit.py does some 
reasonably clever things to try to give an accurate answer including 
varying the number of times it repeats the test to ensure that the time per 
loop is based on a reasonably large time interval.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list