Reputation: 43
(I muddled up the question in the beginning, sorry lol long day.)
I'm pretty much a beginner learning to code in python using the thinkpython pdf. One of the questions asked is to write a function to build a word list using append
and t = t+[x]
. According to my results, append is significantly faster, but I don't have a clue why.
import time
def appended_list():
fin = open('C:\\Users\\Grim\\Documents\\Python\\\Docs\\words.txt','r')
wordlist=[]
for line in fin:
word = line.strip()
wordlist.append(word)
return wordlist
def added_list():
fin = open('C:\\Users\\Grim\\Documents\\Python\\\Docs\\words.txt','r')
wordlist=[]
for line in fin:
word = line.strip()
wordlist= wordlist + [word]
return wordlist
start_t = time.time()
print(len(appended_list()))
end_t = time.time() - start_t
print(end_t)
start_t = time.time()
print(len(added_list()))
end_t = time.time() - start_t
print(end_t)
This is what gets printed:
113809
0.1512610912322998
113809
40.448301792144775
Upvotes: 4
Views: 957
Reputation: 167
The + operation adds another element to the original array. The append operation inserts the array or object into the end of the original array, which results the append uses less memory.
Upvotes: -1
Reputation: 13106
When invoking __add__
which is what +
uses, the two objects being added need to be of the same type
. Think 'a' + 1
, that would raise a TypeError
because they are not the same thing.
With list
, __add__
will need to ensure that whatever you are adding to the list
is also of list
type. By doing so, cls.__add__(obj)
will force obj = [obj]
to occur to facilitate the addition of two like objects. This creates a new list, and there is memory overhead attached to it.
However, append
effectively calls self[len(self):] = obj
. This only modifies the original class and doesn't get any penalty for instantiation of a new list
.
Upvotes: 2
Reputation: 235984
When you use the +
operator, you're creating a new list by concatenating the original list plus another one-element list ([word]
in your case), and there's also the extra effort of copying all the elements from the original lists to the freshly allocated list. Inside a loop, this ends up being O(n^2)
time complexity!
By comparison, append()
modifies the existing list by adding a new element to it, this is much cheaper and uses les memory, because under the hood the list had already allocated more space than needed to accommodate the current number of elements, and only when it's full will it actually grow to a new size, again with room to spare. Inside a loop, this is O(n)
amortized time complexity, way better than the O(n^2)
we obtain by using +
.
Upvotes: 5