Created
September 25, 2020 10:04
-
-
Save payoto/0a1dd9afdbec7793e83da3ee0af22630 to your computer and use it in GitHub Desktop.
This file contains 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
"""Benchmarking of numpy array creation through loops, indexing, list | |
comprehension and generator expressions. | |
Requires: | |
- pip install numpy pytest pytest-benchmark | |
Run with: | |
pytest benchmark_array_creation.py | |
Output of benchmarking: | |
-------------------------------------------- benchmark: 4 tests --------------- | |
Name (time in us) Min Max Mean | |
------------------------------------------------------------------------------- | |
test_generator 4.9000 (1.0) 101.4000 (1.0) 5.3245 (1.0) | |
test_loop 77.3000 (15.78) 275.3000 (2.71) 81.2008 (15.25) | |
test_indexing 79.4000 (16.20) 210.7000 (2.08) 82.5907 (15.51) | |
test_list_comp 80.8000 (16.49) 361.4000 (3.56) 85.3600 (16.03) | |
------------------------------------------------------------------------------- | |
-------------------------------------------------------------------------- | |
Name (time in us) StdDev Median IQR | |
-------------------------------------------------------------------------- | |
test_generator 1.5265 (1.0) 5.1000 (1.0) 0.1000 (1.0) | |
test_loop 10.9756 (7.19) 78.7000 (15.43) 1.5000 (15.00) | |
test_indexing 6.8805 (4.51) 80.8000 (15.84) 1.4000 (14.00) | |
test_list_comp 12.3128 (8.07) 82.1000 (16.10) 1.5000 (15.00) | |
-------------------------------------------------------------------------- | |
------------------------------------------------------------------------- | |
Name (time in us) Outliers OPS (Kops/s) Rounds Iterations | |
------------------------------------------------------------------------- | |
test_generator 1175;1717 187.8125 (1.0) 39526 1 | |
test_loop 299;1166 12.3151 (0.07) 8217 1 | |
test_indexing 296;828 12.1079 (0.06) 5077 1 | |
test_list_comp 361;1220 11.7151 (0.06) 7408 1 | |
------------------------------------------------------------------------- | |
Moral of the story: use generator expressions it is about 15 times faster. | |
""" | |
import numpy as np | |
nodes = np.array( | |
[ | |
[0, 0, 0], | |
[0.5, 0.5, 0.5], | |
[1, 1, 1], | |
[2, 2, 2], | |
[3, 3, 3], | |
[4, 4, 4], | |
[5, 5, 5], | |
[6, 6, 6], | |
] | |
) | |
def test_indexing(benchmark): | |
def indexing(): | |
return np.array( | |
[ | |
np.sqrt(np.sum(np.square(nodes[i + 1] - nodes[i]))) | |
for i in range(nodes.shape[0] - 1) | |
] | |
) | |
length = benchmark(indexing) | |
def test_list_comp(benchmark): | |
def list_comp(): | |
return np.array( | |
[ | |
np.sqrt(np.sum(np.square(end - start))) | |
for start, end in zip(nodes[:-1], nodes[1:]) | |
] | |
) | |
length = benchmark(list_comp) | |
def test_generator(benchmark): | |
def generator(): | |
return np.array( | |
( | |
np.sqrt(np.sum(np.square(end - start))) | |
for start, end in zip(nodes[:-1], nodes[1:]) | |
) | |
) | |
length = benchmark(generator) | |
def test_loop(benchmark): | |
def loop(): | |
length = np.zeros((nodes.shape[0],)) | |
for i in range(nodes.shape[0] - 1): | |
length[i] = np.sqrt(np.sum(np.square(nodes[i + 1] - nodes[i]))) | |
return length | |
length = benchmark(loop) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment