Last active
February 7, 2018 15:11
-
-
Save EthanRosenthal/3e382e35daa94176a7cdbbf441442a9d to your computer and use it in GitHub Desktop.
Lazily stack matrices
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 | |
"""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