Skip to content

Instantly share code, notes, and snippets.

@dtxe
Created March 9, 2021 01:11
Show Gist options
  • Save dtxe/00b601f051302a64e53a5d15b0a48500 to your computer and use it in GitHub Desktop.
Save dtxe/00b601f051302a64e53a5d15b0a48500 to your computer and use it in GitHub Desktop.
Test which implementation of circular buffers is fastest for receiving chunks of streaming data
import numpy as np
from collections import deque
import datetime
iters = 2000
values = np.random.rand(10000)
def nproll():
data = np.zeros(20000)
for kk in range(iters):
data = np.roll(data, -10000)
data[-10000:] = values
def npslice():
data = np.zeros(20000)
for kk in range(iters):
data[:10000] = data[-10000:]
data[-10000:] = values
def usingdeque():
data = deque([0] * 20000, 20000)
for kk in range(iters):
for val in values:
data.append(val)
t = datetime.datetime.now()
nproll()
print('nproll: %.4f' % (datetime.datetime.now() - t).total_seconds())
t = datetime.datetime.now()
npslice()
print('npslice: %.4f' % (datetime.datetime.now() - t).total_seconds())
t = datetime.datetime.now()
usingdeque()
print('deque: %.4f' % (datetime.datetime.now() - t).total_seconds())
#### Results ####
# (base) PS> python .\circ_buffer_speedtest.py
# nproll: 0.0377
# npslice: 0.0156
# deque: 2.5403
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment