Why is + operator much slower than append() when creating a list?
why is the sky blue physics
why is the sky red
why is the sea blue
why is the sun yellow
why is the sky pink
why is the sky blue for kids?
why is the sky blue and sunsets red
(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
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
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!
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
Why is Cats?, But an artificial neural network has picked up on hidden structure in its molecules that may explain why glass is rigid like a solid. Why definition is - for what cause, reason, or purpose. How to use why in a sentence.
__add__ which is what
+ uses, the two objects being added need to be of the same
'a' + 1, that would raise a
TypeError because they are not the same thing.
__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.
append effectively calls
self[len(self):] = obj. This only modifies the original class and doesn't get any penalty for instantiation of a new
Why Is the Sky Blue?, Why is the sky blue? The sky appears blue to the human eye as the short waves of blue light are scattered more than the other colours in Define why. why synonyms, why pronunciation, why translation, English dictionary definition of why. adv. For what purpose, reason, or cause; with what intention, justification, or motive: Why is the door shut?
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.
Why Is Glass Rigid? Signs of Its Secret Structure Emerge., To understand why the sky is blue, we need to consider the nature of sunlight and how it interacts with the gas molecules that make up our atmosphere. Sunlight A difficult problem or question. Used to express mild surprise, indignation, or impatience. [Middle English, from Old English hwȳ; see k w o- in Indo-European roots .] Usage Note: A traditional rule states that why is redundant in the expression reason why, as in The reason why he accepted the nomination is not clear.
Why is the sky blue?, Why definition, for what? for what reason, cause, or purpose?: Why did you behave so badly? for what cause or reason: I don't know why he is leaving. Systems Thinking is the art of considering whole systems of events and interactions rather than simple causal relationships. I recommend Gerald Weinberg's "Introduction to Systems Thinking" (available cheap on the Kindle or expensively 2nd hand) or Peter M. Senge's "The Fifth Discipline".
Terry Moore: Why is 'x' the unknown?, Big Sur coastline looking north to Bixby Canyon Bridge in California. The ocean is blue because water absorbs colors in the red part of the light spectrum. Like a This is why we see a blue sky most of the time. Closer to the horizon, the sky fades to a lighter blue or white. The sunlight reaching us from low in the sky has passed through even more air than the sunlight reaching us from overhead.
Why is the sky blue?, Why is the disease being called coronavirus disease 2019, COVID-19? Can the virus that causes COVID-19 be spread through food, Our money expert Jini Thornton explains why #BlackOutDay2020 is important beyond one day. The black dollar is important to the community and why it’s important to shop black. In this time, it’s essential that we take a stance with our dollars and make a difference at the check out aisle.
- Other way around -
+is the slow one.
- Don't your results show the opposite? You're calling
appended_listfirst in your code.
- @PatrickHaugh dang it, sorry about this. I'll try editing my question in a minute I mean't the opposite aswell.
- Not an answer, but you an also do
wordlist = list(map(str.strip, fin))
- @tobias_k thanks. Never come across map function before ill look into more. but the result is much faster.
- In fact, with
x = x + [y]you are even creating two new lists.
x += [y]o.t.o.h. should act like
extend, but you still create a one-elemented list around
- Yup, that's a good point. Added it to my answer, thanks @tobias_k !
- @tobias_k although that by itself doesn't contribute to algorithmic complexity, and I would expect it to have only a minor runtime effect (the CPython private heap is very good at allocating small lists/dicts etc, and re-uses a lot of memory)
- Thanks so much! that makes so much sense.
- Yup, that sounds better :)
- What do you mean by "adds the array elements to the original array"? That makes it sound like the original list is modified by the operation.