Skip to content

Instantly share code, notes, and snippets.

@nschloe
Created January 17, 2022 21:02
Show Gist options
  • Select an option

  • Save nschloe/5ed4de72f3f9e22ec54fee81e4b14c63 to your computer and use it in GitHub Desktop.

Select an option

Save nschloe/5ed4de72f3f9e22ec54fee81e4b14c63 to your computer and use it in GitHub Desktop.
np.array() vs np.column_stack
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()
@nschloe

nschloe commented Jan 17, 2022

Copy link
Copy Markdown
Author

When concatenating arrays, np.array is faster than np.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.

out

out2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment