Skip to content

Instantly share code, notes, and snippets.

@lukewendling
Created March 3, 2018 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukewendling/3fa48c5bceb4ae0f859b17005817b825 to your computer and use it in GitHub Desktop.
Save lukewendling/3fa48c5bceb4ae0f859b17005817b825 to your computer and use it in GitHub Desktop.
Numba + Numpy benchmark
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.
@lukewendling
Copy link
Author

I guess this is something you'd want to use if you're all into 600x performance improvement and stuff or whatever.

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