Created
May 28, 2018 05:39
-
-
Save oneroyalace/e42e2aaaedc0e88cc76b69fb7028bc0e to your computer and use it in GitHub Desktop.
Are numpy arrays really faster than python lists? Yes they are
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import timeit | |
std_arr = list(range(0,10000)) | |
np_arr = np.array(std_arr) | |
#np_arr_vec = np.vectorize(lambda n: (n * 31) / 31) | |
def operate_on_std_array(): | |
for index,elem in enumerate(std_arr): | |
std_arr[index] = elem * 31 | |
std_arr[index] = elem / 31 | |
return std_arr | |
def operate_on_np_arr(): | |
global np_arr | |
np_arr *= 31 | |
np_arr //= 31 # integer division, not double | |
return np_arr | |
import time | |
def test_time(f): | |
count = 100 | |
start = time.time() | |
for i in range(count): | |
f() | |
dur = time.time() - start | |
return dur | |
print('standard operation', test_time(operate_on_std_array)) | |
print('numpy operation', test_time(operate_on_np_arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment