0

I was trying to do some simple procedures using lists. From the book learning python I saw the method of using a comprehension. Well I also knew that a loop could replace it. Now I really want to know that which is faster, loop or comprehension. These are my programs.

a = []
for x in range(1, 101):
    a.append(x)

This would set a as [1, 2, 3, ......, 99, 100]

Now this is what I have done with the comprehension.

[x ** 2 for x in a]

This is what I did with the loop.

c = []
for x in a:
    b=[x**2]
    c+=b

Could any one say a way to find which of the above is faster.Please also try to explain that how comprehensions differ from loops. Any help is appreciated.

  • 2
    http://stackoverflow.com/questions/1593019/is-there-any-simple-way-to-benchmark-python-script and http://stackoverflow.com/questions/6563314/understanding-list-comprehension-vs-traditional-loop-and-build – Amadan Mar 31 '15 at 05:56
  • List comprehensions tend to be a little faster. This question has some of the low-level technical details: http://stackoverflow.com/q/22108488/1558022 – alexwlchan Mar 31 '15 at 05:59
  • 1
    Dude, instead of asking which is faster just measure it. – freakish Mar 31 '15 at 06:00
  • 1
    The overhead of constructing the extra list and using list addition is going to be slower than either the list comprehension or `list.append(x**2)` But as mentioned the difference between a loop with append and a list comprehension tends to favour the comprehension but not by much. – AChampion Mar 31 '15 at 06:03

1 Answers1

1

You can use the timeit library, or just use time.time() to time it yourself:

>>> from time import time
>>> def first():
...     ftime = time()
...     _foo = [x ** 2 for x in range(1, 101)]
...     print "First", time()-ftime
... 
>>> def second():
...     ftime = time()
...     _foo = []
...     for x in range(1, 101):
...             _b=[x**2]
...             _foo+=_b
...     print "Second", time()-ftime
... 
>>> first()
First 5.60283660889e-05
>>> second()
Second 8.79764556885e-05
>>> first()
First 4.88758087158e-05
>>> second()
Second 8.39233398438e-05
>>> first()
First 2.8133392334e-05
>>> second()
Second 7.29560852051e-05
>>> 

Evidently, the list comprehension runs faster, by a factor of around 2 to 3.

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • I run python 3.x and it returns `First 0.0` and `Second 0.0` all the time.I was able to understand that the code given above is python 2.x.Could you help me in converting it. – theunixdisaster Mar 31 '15 at 06:28
  • The `timeit` library worked fine.I was able to see a huge difference in speed.Thanks a lot. – theunixdisaster Mar 31 '15 at 06:41
  • 1
    The second is much slower because of what's happening inside the loop: it's allocating a new list `[x**2]`, which is then concatenated with the value of `_foo` (allocating a second new list), and assigning the result to `_foo`. A similar trial to the second with `_foo.append(x ** 2)` as the body of the loop yields times nearly identical to the first. – millerdev May 25 '17 at 21:18