473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

List comprehensions performance

I have a doubt regarding list comprehensions:
According to Mark Lutz in his book Learning Pyhon:

"...there is currently a substantial performance advantage to the
extra complexity in this case: based on tests run under Python 2.2,
map calls are roughly twice as fast as equivalent for loops, and list
comprehensions are usually very slightly faster than map. This speed
difference owes to the fact that map and list comprehensions run at C
language speed inside the interpreter, rather than stepping through
Python for loop code within the PVM."

but I also read in Python Performance Tips by Skip Montanaro that
Lists comprehensions are not generally faster than the for loop
version.

What I'd like to know is if using list comprehensions would give me a
performance advantage over traditional for loops or not.
I'm getting fond of list comprehensions, but I wonder if it's wise to
abuse of them...
Jul 18 '05 #1
9 2330

Neuruss> What I'd like to know is if using list comprehensions would
Neuruss> give me a performance advantage over traditional for loops or
Neuruss> not.

You can always run tests to see. <wink> There is some data dependency so it
makes sense to test map, listcomps and for loops using data that's typical
of the application you intend to use them in.

Skip
Jul 18 '05 #2
Yup, use the standard timeit module for testing.
Made for these purposes. .
http://epydoc.sourceforge.net/stdlib...it-module.html

Cheers!
Remco

Skip Montanaro wrote:
Neuruss> What I'd like to know is if using list comprehensions would
Neuruss> give me a performance advantage over traditional for loops or
Neuruss> not.

You can always run tests to see. <wink> There is some data dependency so it
makes sense to test map, listcomps and for loops using data that's typical
of the application you intend to use them in.

Skip

Jul 18 '05 #3
[Neuruss] What I'd like to know is if using list comprehensions would give me a
performance advantage over traditional for loops or not.
For Py2.4, list comprehensions are much faster than equivalent for-loops.

I'm getting fond of list comprehensions, but I wonder if it's wise to
abuse of them...


Use whatever is clearest.
Don't abuse anything.

Raymond Hettinger
Jul 18 '05 #4
Neuruss <lu****@gmx.net> wrote:
...
What I'd like to know is if using list comprehensions would give me a
performance advantage over traditional for loops or not.
Probably, a little, depending on the exact Python release you're using,
and on exactly what you're doing -- as others have suggested, timeit.py
can help. Don't expect _dramatic_ differences.
I'm getting fond of list comprehensions, but I wonder if it's wise to
abuse of them...


No, by definition of "abuse". Use list comprehensions to make lists,
not instead of perfectly normal loops. Consider for example:

kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.25e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.29e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
1000 loops, best of 3: 1.45e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
1000 loops, best of 3: 1.44e+03 usec per loop

So, in this case, abusing list comprehensions slows you down by over
10%.

More generally, _who cares_ about this kind of speedups?! Premature
optimization is the root of all evil in programming. Make your programs
clear, simple, readable -- that's ALWAYS important! So is using general
algorithms and data structures with good O() characteristics if the
input size is liable to grow a lot. But squeezing out 10% or 20% here
or there is going to matter in a TINY minority of cases. Don't distort
your coding for such purposes!
Alex
Jul 18 '05 #5
Raymond Hettinger <vz******@verizon.net> wrote:
[Neuruss] What I'd like to know is if using list comprehensions would give
me a > performance advantage over traditional for loops or not.

For Py2.4, list comprehensions are much faster than equivalent for-loops.


....but if the for loop is NOT equivalent (it doesn't accumulate results
into a resulting list), it's still faster. As I posted:

kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.29e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
1000 loops, best of 3: 1.45e+03 usec per loop

the LC is paying for the building of a list of 999 references to None,
which the for loop easily avoids, so the for loop is much faster here.

Of course, it WAS way more pronounced in 2.3:

kallisti:~/cb alex$ python2.3 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.76e+03 usec per loop
kallisti:~/cb alex$ python2.3 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
100 loops, best of 3: 2.43e+03 usec per loop

I'm getting fond of list comprehensions, but I wonder if it's wise to
abuse of them...


Use whatever is clearest.
Don't abuse anything.


Absolutely. And: use 2.4 -- note that the SLOWER solution in 2.4 still
gains a good 20% over the FASTER one in 2.3... anybody who cares about
performance should be giving 2.4 an intense workout already, rarin' for
the day in which it will be officially released so it can be sensibly
used in customer-delivered software... (and, for those not in the know,
I should point out that Raymond was the one most responsible for the
overall impressive speed-up 2.2->2.3->2.4...!!!-).
Alex
Jul 18 '05 #6
Thank you guys!
I will investigate the timeit module as suggested for these kind of tests...
Jul 18 '05 #7
On Thu, 30 Sep 2004 10:55:52 +0200, al*****@yahoo.com (Alex Martelli) wrote:
Raymond Hettinger <vz******@verizon.net> wrote:
[Neuruss] What I'd like to know is if using list comprehensions would give
me a > performance advantage over traditional for loops or not.

For Py2.4, list comprehensions are much faster than equivalent for-loops.


...but if the for loop is NOT equivalent (it doesn't accumulate results
into a resulting list), it's still faster. As I posted:

kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.29e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
1000 loops, best of 3: 1.45e+03 usec per loop

the LC is paying for the building of a list of 999 references to None,
which the for loop easily avoids, so the for loop is much faster here.


What if you abuse the LC so it makes an empty list? E.g.,
[None for x in xrange(999) if f() and False]

Not that I'm trying to promote LC abuse ;-)

Regards,
Bengt Richter
Jul 18 '05 #8
Bengt Richter <bo**@oz.net> wrote:
...
...but if the for loop is NOT equivalent (it doesn't accumulate results
into a resulting list), it's still faster. As I posted:

kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
xrange(999): f()'
1000 loops, best of 3: 1.29e+03 usec per loop
kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
xrange(999)]'
1000 loops, best of 3: 1.45e+03 usec per loop

the LC is paying for the building of a list of 999 references to None,
which the for loop easily avoids, so the for loop is much faster here.


What if you abuse the LC so it makes an empty list? E.g.,
[None for x in xrange(999) if f() and False]


Now tell me, is timing so HARD...?! This obfuscation times (on 2.4 and
the same old iBook as above) to 1.38e+03 usec per loop, still slower
than the plain old loop -- the if/and rigmarole costs...
Alex
Jul 18 '05 #9
On Thu, 30 Sep 2004 20:13:04 +0200, al*****@yahoo.com (Alex Martelli) wrote:
Bengt Richter <bo**@oz.net> wrote:
...
>...but if the for loop is NOT equivalent (it doesn't accumulate results
>into a resulting list), it's still faster. As I posted:
>
>kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' 'for x in
>xrange(999): f()'
>1000 loops, best of 3: 1.29e+03 usec per loop
>kallisti:~/cb alex$ python2.4 timeit.py -s'def f():pass' '[f() for x in
>xrange(999)]'
>1000 loops, best of 3: 1.45e+03 usec per loop
>
>the LC is paying for the building of a list of 999 references to None,
>which the for loop easily avoids, so the for loop is much faster here.


What if you abuse the LC so it makes an empty list? E.g.,
[None for x in xrange(999) if f() and False]


Now tell me, is timing so HARD...?! This obfuscation times (on 2.4 and
the same old iBook as above) to 1.38e+03 usec per loop, still slower
than the plain old loop -- the if/and rigmarole costs...

Sure, I was just wondering what would happen if you traded that for list growth overhead.
Anyway, thanks. It _is_ hard to compare with someone else's previous timing numbers ;-)
Maybe timeit.py should have an option for bogomips normalization or returning
time*bogomips. My system needs an upgrade ;-)

Regards,
Bengt Richter
Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Elaine Jackson | last post by:
List comprehensions don't work the way you intuitively expect them to work. I realize many people have no intuitions about how list comprehensions 'should' work, so if you recognize yourself in...
24
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I...
23
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit...
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
7
by: Steven Bethard | last post by:
Tom Anderson <twic@urchin.earth.li> wrote: > Sounds good. More generally, i'd be more than happy to get rid of list > comprehensions, letting people use list(genexp) instead. That would >...
6
by: Lonnie Princehouse | last post by:
List comprehensions appear to store their temporary result in a variable named "_" (or presumably "_", "_" etc for nested comprehensions) In other words, there are variables being put into the...
31
by: John Salerno | last post by:
I posted this code last night in response to another thread, and after I posted it I got to wondering if I had misused the list comprehension. Here's the two examples: Example 1:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.