Created
January 17, 2022 21:02
-
-
Save nschloe/5ed4de72f3f9e22ec54fee81e4b14c63 to your computer and use it in GitHub Desktop.
np.array() vs np.column_stack
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 perfplot | |
import numpy as np | |
rng = np.random.default_rng(0) | |
def setup(n): | |
return [rng.random(n) for _ in range(10)] | |
def column_stack(data): | |
return np.column_stack(data) | |
def array(data): | |
return np.array(data) | |
b = perfplot.bench( | |
setup=setup, | |
kernels=[column_stack, array], | |
n_range=[2 ** k for k in range(20)], | |
equality_check=None, | |
) | |
b.save("out.png") | |
b.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
When concatenating arrays,
np.array
is faster thannp.column_stack
by a factor of about 3 to 5.Reason: For
np.array
, the memory doesn't need to rearranged. The array data is just copied into one place.