[PYTHON] It's faster to add than to join and extend the list, right?

Easy speed measurement

Let's measure the speed when adding sub_list = list (range (10000)) to an empty list.

1. ʻextend` speed

In:
%%timeit

li = []
li.extend(sub_list)
Out:
26.7 µs ± 2.44 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

2. Addition speed

In:
%%timeit

li = []
li += sub_list
Out:
25.3 µs ± 281 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

** Addition is faster and speed is stable. ʻExtend` is a built-in function, but it's slow, isn't it? ** **

Detailed speed measurement

For this problem,

--Original list length --Length of list to add

Since there are two variables, measure the speed when you play with them. Then, subtract the speeds and see which of ʻextend` and addition wins.

If the list is short

--Vertical axis: Length of original list --Horizontal axis: Length of list to be added --White: ʻextend` is faster --Black: Addition is faster

image.png

** Addition advantage **

If the list is long

--Vertical axis: Length of original list --Horizontal axis: Length of list to be added --White: ʻextend` is faster --Black: Addition is faster

image.png

** ʻextend` dominance **

I wonder if it's involved in the implementation of Python in the first place?

code

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
from time import time


def extend_time(old_i, new_i):
    old = list(range(10 ** old_i))
    new = list(range(10 ** new_i))
    s = time()
    old.extend(new)
    e = time()
    return e - s


def add_time(old_i, new_i):
    old = list(range(10 ** old_i))
    new = list(range(10 ** new_i))
    s = time()
    old += new
    e = time()
    return e - s


extend_time = np.vectorize(extend_time)
add_time = np.vectorize(add_time)


shape = (8, 8)
extend_graph = np.fromfunction(extend_time, shape, dtype=np.int)
add_graph = np.fromfunction(add_time, shape, dtype=np.int)

result = np.where(extend_graph - add_graph > 0, True, False)
sns.heatmap(result, cbar=False, xticklabels=[10 ** i for i in range(shape[0])], yticklabels=[10 ** i for i in range(shape[0])])
plt.ylabel("old list length")
plt.xlabel("new list length")
plt.show()

Recommended Posts

It's faster to add than to join and extend the list, right?
Python> list> append () and extend ()> append: list is added | extend: list elements are added | + = to add list
Add disks to extend LVM SWAP and / home area
[pyqtgraph] Add region to the graph and link it with the graph region
Attempt to extend a function in the library (add copy function to pathlib)
Remove and retrieve arrays from fasta according to the ID list file
Memorandum (Add name only to people with the same surname in the list)
Ford-Fulkerson Method and Its Applications-Supplement to Chapter 8 of the Algorithm Quick Reference-
[Introduction to Python] What is the difference between a list and a tuple?
It's really useful to add save () and load () methods to Target in Luigi