I need to get performance optimization extremely with python, numpy.
My data is like this,
a1 = np.array(np.random.random(500000) * 1000)
a2 = np.array(np.random.random(500000) * 5000)
With different ndarray a1, a2, I want to calculate min-max gap.
numpy:
np.max(a1) - np.min(a2)
numba:
@nb.jit(nb.float64(nb.float64, nb.float64), cache=True, fastmath=True)
def nb_max_min(s1, s2):
return np.max(s1) - np.min(s2)
But, I got disappointed result
min-max(numba): 1.574092000000249 ms
max-max(numpy): 1.4246419999999205 ms
I want to make more fast calc within ~0.xx ms if possible. How to conquer this optimization?
update
I only measured max - min part. My timing code is here.
import time
def timing(label, fn):
t0 = time.perf_counter()
fn()
t1 = time.perf_counter()
print('{}: {} ms'.format(label, (t1 - t0) * 1000))
All my code here,
@nb.jit(nb.float64(nb.float64, nb.float64), cache=True, fastmath=True)
def nb_max_min(s1, s2):
return np.max(s1) - np.min(s2)
a1 = np.random.random(periods) * 2000
a2 = np.random.random(periods) * 1000
timing('nb_min_max', lambda: nb_max_min(a1, a2))
timing('nb_min_max', lambda: nb_max_min(a1, a2))
timing('nb_min_max', lambda: nb_max_min(a1, a2))
timing('max-max', lambda: np.max(a1) - np.min(a2))
timing('max-max', lambda: np.max(a1) - np.min(a2))
timing('max-max', lambda: np.max(a1) - np.min(a2))
And, this is result
nb_min_max: 0.728947999999896 ms
nb_min_max: 1.0030130000000526 ms
nb_min_max: 1.3124690000001493 ms
max-max: 1.662436000000156 ms
max-max: 0.9315169999997153 ms
max-max: 1.9570019999992638 ms
also I tried timeit
%timeit np.max(a1) - np.min(a2)
475 µs ± 9.72 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
I think this is the most fastest way with python. Numpy or numba's result is not different significantly. As user2699 commented, fortran is the last chance to optimize..