import numpy as np
import timeit
# решение с mgrid
def solution_mgrid():
mat_A, _ = np.mgrid[0:100:5, 0:100:5]
return mat_A.T
# решение с tile
def solution_tile():
col_vector = np.arange(0, 100, 5).reshape(-1, 1)
return np.tile(col_vector, (1, 20))
# Количество повторений
iterations = 100000
# Замер времени для первого решения
time_mgrid = timeit.timeit(solution_mgrid, number=iterations)
# Замер времени для второго решения
time_tile = timeit.timeit(solution_tile, number=iterations)
print(f'Time for mgrid solution: {time_mgrid:.6f} seconds')
print(f'Time for tile solution: {time_tile:.6f} seconds')
Time for mgrid solution: 1.846957 seconds
Time for tile solution: 0.569512 seconds