Однако, когда я написал несколько тестовых примеров, результаты меня удивили. У меня есть два вопроса:
- Мои результаты показывают, что основной порядок массива, похоже, не влияет на скорость доступа, что неожиданно. Почему это происходит?
- Я протестировал два метода доступа к массиву: доступ на основе индекса и итерацию. Я ожидал, что они будут иметь одинаковую производительность, поскольку оба имеют одинаковую сложность. Однако мои результаты показывают, что итерационный метод превосходит доступ на основе индекса. Почему это так?
< pre class="lang-py Prettyprint-override">from time import perf_counter
import numpy as np
def test_column_access_numpy_index(array):
n_row, n_col = array.shape
start = perf_counter()
for j in range(n_col):
for i in range(n_row):
_ = array[i, j]
return perf_counter() - start
def test_row_access_numpy_index(array):
n_row, n_col = array.shape
start = perf_counter()
for i in range(n_row):
for j in range(n_col):
_ = array[i, j]
return perf_counter() - start
def test_column_access_numpy_iteration(array):
n_row, n_col = array.shape
start = perf_counter()
for j in range(n_col):
for item in array[:, j]:
pass
return perf_counter() - start
def test_row_access_numpy_iteration(array):
n_row, n_col = array.shape
start = perf_counter()
for i in range(n_row):
for item in array:
pass
return perf_counter() - start
if __name__=='__main__':
size = 10_000
row_major = np.ones((size, size), dtype=np.float32, order='C')
col_major = np.ones((size, size), dtype=np.float32, order='F')
print("Warm up")
test_row_access_numpy_iteration(row_major)
print("Input row major")
time = test_row_access_numpy_index(row_major)
print(f"Testing row access index in numpy: {time:.6f} seconds")
time = test_column_access_numpy_index(row_major)
print(f"Testing column access index in numpy: {time:.6f} seconds")
time = test_row_access_numpy_iteration(row_major)
print(f"Testing row access iteration in numpy: {time:.6f} seconds")
time = test_column_access_numpy_iteration(row_major)
print(f"Testing column access iteration in numpy: {time:.6f} seconds")
print('----------------------------')
print("Input col major")
time = test_row_access_numpy_index(col_major)
print(f"Testing row access index in numpy: {time:.6f} seconds")
time = test_column_access_numpy_index(col_major)
print(f"Testing column access index in numpy: {time:.6f} seconds")
time = test_row_access_numpy_iteration(col_major)
print(f"Testing row access iteration in numpy: {time:.6f} seconds")
time = test_column_access_numpy_iteration(col_major)
print(f"Testing column access iteration in numpy: {time:.6f} seconds")
Вот результат
Warm up
Input row major
Testing row access index in numpy: 7.732731 seconds
Testing column access index in numpy: 8.025850 seconds
Testing row access iteration in numpy: 3.111501 seconds
Testing column access iteration in numpy: 3.129321 seconds
----------------------------
Input col major
Testing row access index in numpy: 7.852834 seconds
Testing column access index in numpy: 7.978318 seconds
Testing row access iteration in numpy: 3.027528 seconds
Testing column access iteration in numpy: 3.075494 seconds
Подробнее здесь: https://stackoverflow.com/questions/793 ... ray-access