Efficient python programming...

Brian McErlean b_mcerlean at yahoo.com
Fri Jun 7 10:10:59 EDT 2002


gabor <gabor at realtime.sk> wrote in message news:<mailman.1023401650.18434.python-list at python.org>...
> 
> for file in directory:
> 	file.printsize()
> 
> 
> ran = range(len(directory))
> for r in ran:
> 	directory[r].printsize()
> 
> [file.printsize() for file in directory]
> 
> they're the same, and everyone would recommend you the last one..

I wouldn't.  The first one is better in this case.  Using the list
comprehension implys to me that you're interested in the result -
while in fact you're discarding it.  If there are a lot of files in
the directory (And after all, thats the only reason to optimise it),
then you'll end up with a large list of [None,None,None,...] that is
completely unneeded.

testing gives:

[foo(i) for i in range(100000)]
- took 0.240000009537 secs

for i in range(100000): foo(i)
- took 0.149999976158 secs

(foo(i) is just an empty function)

Using the list comprehension here is both misleading (because you are
relying on a side-effect, not using the returned list), and less
efficient.  Using the for loop is better, because it makes clear that
you're using an imperative technique, relying on the side-effect
(printing the size) and are uninterested in building a list at all.

On the other hand, if the code is:

l=[]
for file in directory:
 	l.append(file.getsize())

It is both clearer, and more efficient if implemented as:
l= [file.getsize() for file in directory]

Brian.



More information about the Python-list mailing list