I wrote a function which is too time consuming when used with for loops. It appends numpy vectors (10,0) as rows in each iteration. How can I use a vectorized numpy solution for the iterations to speed this up?
Any hint why the vstack-array solution below is even slower than the append-list solution?
TIA
import numpy as np
import time
n_iterations = 1000
n_cols = 10
def sample_func():
# Addition: please notice: the randon function is not important. It is only an example function. The real function is more complex and needs to replace for loops by a faster numpy solution.
row = np.random.rand(0,n_cols)
return row
#list solution: too slow
start_time_1 = time.time()
result_list = []
for i in range(n_iterations):
result_row = sample_func()
result_list.append(np.sort(result_row))
print("Run time = {}".format(time.time() - start_time_1))
#array solution: too slow
start_time_2 = time.time()
result_array = np.empty([0,n_cols])
for i in range(n_iterations):
result_row = sample_func()
result_array = np.vstack([result_array, np.sort(result_row)])
print("Run time = {}".format(time.time() - start_time_2))
TIA
np.sortdoes allow sort along an axis.vstackmakes a new array with full copy. Use it just once to join a whole list arrays, not incrementally.