# -*- coding: utf-8 -*- """ Test av to ulike metoder for listeutvidelse med ett element Finn Aakre Haugen (finn.haugen@usn.no) University of South-Eastern Norway 2019 06 10 """ # ---------------------------------------------------------- import numpy as np import time # ---------------------------------------------------------- N = 100000 dyn_array = np.array([]) preallok_array = np.zeros(N) # ---------------------------------------------------------- # append: tic = time.time() for k in range(0, N): dyn_array = np.append(dyn_array, 10*k) toc = time.time() t_elapsed_append = toc - tic # ---------------------------------------------------------- # preallok: tic = time.time() for k in range(0, N): preallok_array[k] = 10*k toc = time.time() t_elapsed_preallok = toc - tic # ---------------------------------------------------------- # Sammenlikning: forhold_append_preallok = t_elapsed_append/t_elapsed_preallok print('------------------------------------------------') print('N =', N) #print('t_elapsed_append [s] = {:.5f}'.format(t_elapsed_append)) #print('t_elapsed_preallok [s] = {:.5f}'.format(t_elapsed_preallok)) #print('t_elapsed_append/t_elapsed_preallok = {:.5f}'.format(forhold_append_preallok)) print('t_elapsed_append [s] = ',t_elapsed_append) print('t_elapsed_preallok [s] =', t_elapsed_preallok) print('t_elapsed_append/t_elapsed_preallok =', forhold_append_preallok) print('------------------------------------------------')