Last active
December 19, 2015 16:29
-
-
Save njsmith/5984557 to your computer and use it in GitHub Desktop.
Measuring the per-item time for various numpy operations, after correcting for overhead
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
# Example output: | |
# a + a: 1.181 ns/item | |
# a / a: 2.577 ns/item | |
# a ** a: 15.259 ns/item | |
# np.log(a): 28.241 ns/item | |
# np.sin(a): 22.202 ns/item | |
# sp.gammaln(a): 40.876 ns/item | |
# sp.erf(a): 21.297 ns/item | |
import timeit | |
LOOPS = 1000000 | |
ARR_SIZES = [1, 1001] | |
SETUP_CODE = ("import numpy as np; " | |
"import scipy.special as sp; " | |
"a = np.asarray([1.] * %r)") | |
OPS = ["a + a", | |
"a / a", | |
"a ** a", | |
"np.log(a)", | |
"np.sin(a)", | |
"sp.gammaln(a)", | |
"sp.erf(a)", | |
] | |
for op in OPS: | |
times = [] | |
for arr_size in ARR_SIZES: | |
setup = SETUP_CODE % (arr_size,) | |
times.append(timeit.timeit(op, setup=setup, number=LOOPS) / LOOPS) | |
print("%s: %0.3f ns/item" | |
% (op, | |
(times[1] - times[0]) * 1.0 | |
/ (ARR_SIZES[1] - ARR_SIZES[0]) * 1e9, | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment