Skip to content

Instantly share code, notes, and snippets.

@EthanRosenthal
Last active February 7, 2018 15:11
Show Gist options
  • Save EthanRosenthal/3e382e35daa94176a7cdbbf441442a9d to your computer and use it in GitHub Desktop.
Save EthanRosenthal/3e382e35daa94176a7cdbbf441442a9d to your computer and use it in GitHub Desktop.
Lazily stack matrices
import numpy as np
"""Stacking as we loop"""
%%timeit -n 10
res = None
for i in range(100):
batch = np.random.random((100, 100))
if res is None:
res = batch
else:
res = np.vstack((res, batch))
# 279 ms ± 15 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
"""Stacking after the loop"""
%%timeit -n 10
res = []
for i in range(100):
res.append(np.random.random((100, 100)))
res = np.vstack(res)
# 14 ms ± 657 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment