Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed of using append in python repeatedly

Is it faster substantially to start with a preallocated list and set items at each index, as opposed to starting with an empty list and appending items? I need this list to hold 10k-100k items.

I ask because I am trying to implement an algorithm that requires O(n) time at each level of recursion, but I am getting results that indicate O(n^2) time. I thought perhaps python needing to keep resizing the list might cause this slowdown.

I found similar questions but none that explicitly answered my question. One answer indicated that garbage collecting might be very slow with so many items, so I tried turning gc on and off at saw no improvement in results.

PROBLEM SOLVED: If anyone is curious, the slowdown was caused by unioning sets together too often. Now I use a different method (involves sorting), to check if the same key is seen twice.

like image 813
Tyler Brabham Avatar asked Oct 09 '13 23:10

Tyler Brabham


People also ask

Does append take a lot of time in Python?

append() can cause python to allocate more memory, which takes time.

Is append in Python slow?

append occasionally will have to copy all the elements of a list to a bigger list, . append must be slower than pre-allocating the entire length of the list and assigning to individual indices of that list (of course you can only do that if you know how big you will need the list to be beforehand).

Is append faster than extend Python?

Let's create a list of 100 numbers and add 1000 items to it. From this, we can see that extend the function is much faster than append. If we want to add a list of items to another list we can use extend function.

Whats faster than append Python?

So when you add items continuously, you should prefer . append() , otherwise you should use . extend() .

How to speed up append in Python?

How to speed up append in Python? def fun -> list: array = ... # creating of array return array def fun2: array2 = [] # some code for i in range (some_value): array2.append (fun) Note that it isn't known how many values function fun returns in each step of the algorithm so it is impossible to allocate array2 at the beginning.

What is append () and extend () in Python?

append() and extend() in Python. Append: Adds its argument as a single element to the end of a list. The length of the list increases by one. Output: NOTE: A list is an object. If you append another list onto a list, the first list will be a single object at the end of the list. Output:

Is list append () in Python faster than appending to a list?

numpy.append () in Python --- faster than appending to a List ? Python numpy append () function is used to merge two arrays. This function returns a new array and the original array remains unchanged. ---------- So it sounds like List.append (new_value) is faster. That's right.

Is NumPy append slower than list append?

Python lists are resizeable therefore appending is very fast. If you intend to append few values (compared to the amount already there) and don't need a new array, then yes, numpy.append should be slower than list 's .append for a large enough array (for N elements in one array and M in the other it'd be O (N + M) compared to amortized O (M)).


1 Answers

Python preallocates the list in chunks that are proportional to the size of the list. This gives amortized O(1) for appending to lists

Here is a simple test to see when a list grows. Note that many of these will be able to be reallocated in place, so a copy over isn't always necessary

>>> import sys
>>> A = []
>>> sz = sys.getsizeof(A)
>>> for i in range(100000):
...     if sz != sys.getsizeof(A):
...         sz = sys.getsizeof(A)
...         print i, sz
...     A.append(i)
... 
1 48
5 64
9 96
17 132
26 172
36 216
47 264
59 320
73 384
89 456
107 536
127 624
149 724
174 836
202 964
234 1108
270 1268
310 1448
355 1652
406 1880
463 2136
527 2424
599 2748
680 3116
772 3528
875 3992
991 4512
1121 5100
1268 5760
1433 6504
1619 7340
1828 8280
2063 9336
2327 10524
2624 11864
2959 13368
3335 15060
3758 16964
4234 19108
4770 21520
5373 24232
6051 27284
6814 30716
7672 34580
8638 38924
9724 43812
10946 49312
12321 55500
13868 62460
15608 70292
17566 79100
19768 89012
22246 100160
25033 112704
28169 126816
31697 142692
35666 160552
40131 180644
45154 203248
50805 228676
57162 257284
64314 289468
72360 325676
81412 366408
91595 412232
like image 183
John La Rooy Avatar answered Sep 22 '22 03:09

John La Rooy