Let's measure the speed when adding sub_list = list (range (10000))
to an empty list.
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)
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? ** **
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.
--Vertical axis: Length of original list --Horizontal axis: Length of list to be added --White: ʻextend` is faster --Black: Addition is faster
** Addition advantage **
--Vertical axis: Length of original list --Horizontal axis: Length of list to be added --White: ʻextend` is faster --Black: Addition is faster
** ʻextend` dominance **
I wonder if it's involved in the implementation of Python in the first place?
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()