Created
March 3, 2018 16:25
-
-
Save lukewendling/3fa48c5bceb4ae0f859b17005817b825 to your computer and use it in GitHub Desktop.
Numba + Numpy benchmark
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
from numba import jit | |
from numpy import arange | |
import numpy as np | |
def sum2d_loop(arr): | |
M, N = arr.shape | |
result = 0.0 | |
for i in range(M): | |
for j in range(N): | |
result += arr[i,j] | |
return result | |
def sum2d_np(arr): | |
return np.sum(arr) | |
@jit | |
def sum2d_loop_numba(arr): | |
M, N = arr.shape | |
result = 0.0 | |
for i in range(M): | |
for j in range(N): | |
result += arr[i,j] | |
return result | |
@jit | |
def sum2d_np_numba(arr): | |
return np.sum(a) | |
a = arange(1000000).reshape(1000,1000) | |
%timeit sum2d_loop(a) | |
%timeit sum2d_np(a) | |
%timeit sum2d_loop_numba(a) | |
%timeit sum2d_np_numba(a) | |
# 179 ms / .3 ms => 600x improvement from python loop compared to np+numba compiled func. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess this is something you'd want to use if you're all into 600x performance improvement and stuff or whatever.