egoburnswell
egoburnswell

Reputation: 87

In python is it better to have a series of += or to append to a list and then sum?

Both of these bits of code do the same thing:

g = 1
g += 2
g += 17
print g

g = []
g.append(1)
g.append(2)
g.append(17)
print sum(g)

I was just wondering if one of these ways is "better" or more Python than the other. My own testing with the following bit of code:

import time

n = 1000000

A = time.clock()
w = 0
for i in range(n):
    w += i
print w, time.clock() - A

A = time.clock()
g = []
for i in range(n):
    g.append( i )
print sum(g), time.clock() - A

seems to indicate that the first method is slightly faster, but I may be missing something. Or I may be missing an entirely better way to perform this type of operation. Any input would be welcome.

Upvotes: 1

Views: 376

Answers (5)

egoburnswell
egoburnswell

Reputation: 87

edit: disregard this, I can see now that it is not generally true, and is only true for specific values of x.

Ok, so I can see that making a list should be inefficient, but then why does fun2 run more quickly in this instance? Doesn't it essentially create a list and then sum over it?

import timeit

def fun1(x):
    w = 0
    for i in range(x):
        w += i
    return w 

def fun2(x):
    return sum([i for i in range(x)])

timer = timeit.Timer(stmt='fun1(10000)', setup='from __main__ import fun1')
print timer.timeit(number=10000)


timer = timeit.Timer(stmt='fun2(10000)', setup='from __main__ import fun2')
print timer.timeit(number=10000)

Upvotes: 0

dbra
dbra

Reputation: 631

Absolutely the first, for many reason, first of all memory allocation (N integer instead just one) and performance: in real world application the GC overhead would pop out.

Upvotes: 1

arshajii
arshajii

Reputation: 129537

It's not a matter of being Pythonic, it's a matter of what you want to achieve.

Do you want to save the values that constitute the sum so they can be referred to later? If so, use a list. If not, then why even bother with a list? It would just be a convoluted and less efficient way to do the same thing -- just add the values up. The direct addition method will obviously be faster because all you're doing is adding to a variable (very cheap), instead of mutating a list (which has a greater cost). Not to mention the evident memory advantage of using the direct addition approach, since you wouldn't be storing useless numbers.

Upvotes: 4

user2555451
user2555451

Reputation:

The only reason to have the second method is if you plan to use the list g elsewhere in the code. Otherwise, there isn't a reason to do it the second way. Making a list and then summing its values is a lot more costly then just incrementing a variable.

Moreover, if incrementing g is your goal, then why not do that? "Explicit is better than implicit" is a motto of Python. The first method explicitly increments g.

Also, the list may confuse people. When they see your code, they will think you need the list and plan to use it elsewhere. Not to mention that g is now a list. If g is supposed to be a number, having it be a list is not good and can lead to problems.

Finally, the first solution has less syntax (always a plus if it does the same job efficiently).

So, I'd go with method 1.

Upvotes: 1

Brionius
Brionius

Reputation: 14118

Method A is

  1. Add the integers

Method B is

  1. Create a list of integers
  2. Add the integers

If all you want to do is

  1. Add the integers

I'd go with method A.

Upvotes: 3

Related Questions