Speed: Add element to end of Python array

1.First of all

There are several ways to add an element to the end of a Python array, depending on whether you use the list type or the numpy ndarray type. I was wondering how much the execution speed would be different by these methods, so when Karukuda looked it up, Comparison of append to list type Only entry / 2014/10/20/110304) was found (I'm sorry if there is insufficient research). Therefore, we will investigate the execution speed of the method of adding elements to the three arrays shown in the next section by a simple experiment. The number of times elements were added was 100,000, and the time module was used for time measurement. The code used in the experiment is placed at the very end.

2. How to add elements to the array

2.1 Method for list type

How to use list.append. This is a standard method built into Python, and should be a method that even beginners of Python often use.

x = []
for i in range( 10 ):
    x.append( i )

2.2 Method for ndarray 1

How to use numpy.append.

import numpy as np
x = np.array( [] )
for i in range( 10 ):
    x = np.append( x, i )

2.3 Method for ndarray 2

How to use numpy.hstack.

import numpy as np
x = np.array( [] )
for i in range( 10 ):
    x = np.hstack( (x,i) )

3 Result

Execution time (seconds)
list.append 0.0149400234222
numpy.append 4.47969698906
numpy.hstack 4.75327301025

I expected it a little, but list.append is overwhelmingly fast. And there is no speed difference between numpy.append and numpy.hstack. After all, if you know the size of the required array in advance, it seems that the correct answer is to use ndarray, otherwise use list.

4 Code used for the experiment

import numpy as np

def stdAppend(n):
    x = []
    for i in range(n):
        x.append(i)
    return

def npAppend(n):
    x = np.array( [] )
    for i in range(n):
        x = np.append(x,i)
    return

def npHstack(n):
    x = np.array( [] )
    for i in range(n):
        x = np.hstack( (x,i) )
    return

if __name__ == '__main__':
    import time

    n = 100000
    
    start = time.time()
    stdAppend(n)
    ttime = time.time() - start
    print( "stdAppend: %s" % ttime )

    start = time.time()
    npAppend(n)
    ttime = time.time() - start
    print( "npAppend : %s" % ttime )

    start = time.time()
    npHstack(n)
    ttime = time.time() - start
    print( "npHstack : %s" % ttime )

Recommended Posts

Speed: Add element to end of Python array
Just add the python array to the json data
[python] How to sort by the Nth Mth element of a multidimensional array
[Python] Manipulation of elements in list (array) [Add / Delete]
Numba to speed up as Python
Subscript access to python numpy array
Add Python 2.7 Japanese documentation to Dash.app
[Python] How to swap array values
How to speed up Python calculations
Speed comparison of Python XML parsing
Add TRACE log level to Python ...?
How to determine the existence of a selenium element in Python
[Python] How to get divisors of natural numbers at high speed
Various ways to create an array of numbers from 1 to 10 in Python.
[Python] Summary of how to use pandas
[Python] Summary of array generation (initialization) time! !! !!
[Python2.7] Summary of how to use unittest
[Python] Add total rows to Pandas DataFrame
Add Gaussian noise to images with python2.7
Python Basic Course (at the end of 15)
How to add python module to anaconda environment
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
Add a Python virtual environment to VSCode
[Python] Add comments to standard input files
[Introduction to Data Scientists] Basics of Python ♬
[Question] How to use plot_surface of python
I made a function to see the movement of a two-dimensional array (Python)
python: Tips for displaying an array (list) with an index (how to find out what number an element of an array is)
Organize Python tools to speed up the initial movement of data analysis competitions
Get the last element of the array by splitting the string in Python and PHP
Want to add type hints to your Python decorator?
[Introduction to Udemy Python 3 + Application] 26. Copy of dictionary
Correspondence summary of array operation of ruby and python
[Python] How to use two types of type ()
Convert "number" of excel date to python datetime
Add rows to an empty array with numpy
[Introduction to Udemy Python 3 + Application] 19. Copy of list
[Python3] Coarse graining of numpy.ndarray Speed comparison etc.
Summary of how to use MNIST in Python
How to add a Python module search path
Compare the speed of Python append and map
Send Gmail at the end of the process [Python]
How to specify attributes with Mock of python
To add a module to python put in Julialang
How to get dictionary type elements of Python 2.7
[Python numpy] Dynamically specify the index of the array
Output in the form of a python array
Calculation speed of indexing for numpy quadratic array
Batch conversion of Excel files to JSON [Python]
Summary of studying Python to use AWS Lambda
List of Python code to move and remember
Remove specific strings at the end of python
[Python] Do your best to speed up SQLAlchemy
Speed evaluation of CSV file output in Python
[Introduction to Python] Basic usage of lambda expressions
Updated to Python 2.7.9
Introduction of Python
How to identify the element with the smallest number of characters in a Python list?
Basics of Python ①
Basics of python ①