Skip to content

Instantly share code, notes, and snippets.

@cchwala
Created July 19, 2016 15:55
Show Gist options
  • Save cchwala/dea03fb55d9a50660bd52e00f5691db5 to your computer and use it in GitHub Desktop.
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
def my_roll(arr, x):
arr[0:-1] = arr[1:]
arr[-1] = x
return arr
%%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
%%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
%%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