Created
July 19, 2016 15:55
-
-
Save cchwala/dea03fb55d9a50660bd52e00f5691db5 to your computer and use it in GitHub Desktop.
Speed of numpy.roll vs list with append and pop vs custom numpy rolling using slices
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
def my_roll(arr, x): | |
arr[0:-1] = arr[1:] | |
arr[-1] = x | |
return arr |
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
%%timeit | |
x_list = range(100000) | |
x_arr = np.array(x_list) | |
for i in range(1000): | |
x_arr = my_roll(x_arr, 101) | |
# On my machine: 10 loops, best of 3: 52.3 ms per loop | |
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
%%timeit | |
x_list = range(100000) | |
x_arr = np.array(x_list) | |
for i in range(1000): | |
x_list.append(1) | |
x_list.pop(0) | |
# On my machine: 10 loops, best of 3: 53.5 ms per loop |
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
%%timeit | |
x_list = range(100000) | |
x_arr = np.array(x_list) | |
for i in range(1000): | |
x_arr = np.roll(x_arr, -1) | |
x_arr[-1] = 1 | |
# On my machine: 1 loop, best of 3: 288 ms per loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment