Skip to content

Instantly share code, notes, and snippets.

@pitrou
Created December 8, 2014 17:24
Show Gist options
  • Save pitrou/878ae05c803cc2230370 to your computer and use it in GitHub Desktop.
Save pitrou/878ae05c803cc2230370 to your computer and use it in GitHub Desktop.
from __future__ import absolute_import, print_function, division
import math
import numpy as np
from numba import jit, vectorize
from numba.utils import benchmark
arr_f32 = np.linspace(0, 1, 1e5, dtype=np.float32)
arr_f64 = np.linspace(0, 1, 1e5, dtype=np.float64)
out_f32 = arr_f32.copy()
out_f64 = arr_f64.copy()
def _arr_align(arr):
return arr.ctypes.data & 31
print("f32 align: arr = %d, out = %d"
% (_arr_align(arr_f32), _arr_align(out_f32)))
print("f64 align: arr = %d, out = %d"
% (_arr_align(arr_f64), _arr_align(out_f64)))
@vectorize(['float32(float32)', 'float64(float64)'])
def nb_cos(x):
return math.exp(x)
@vectorize(['float32(float32, float32)', 'float64(float64, float64)'])
def nb_add(x, y):
return x + y
@vectorize(['float32(float32, float32)', 'float64(float64, float64)'])
def nb_stuff(x, y):
return x - y * abs(x)
def bench(f):
bench._benchmarks.append(f)
return f
bench._benchmarks = []
#@bench
def numba_stuff_f32():
nb_stuff(arr_f32, arr_f32, out_f32)
#@bench
def numba_stuff_f64():
nb_stuff(arr_f64, arr_f64, out_f64)
#@bench
def python_add_f32():
np.add(arr_f32, arr_f32, out_f32)
@bench
def numba_add_f32():
nb_add(arr_f32, arr_f32, out_f32)
#@bench
def python_add_f64():
np.add(arr_f64, arr_f64, out_f64)
@bench
def numba_add_f64():
nb_add(arr_f64, arr_f64, out_f64)
#@bench
def python_cos_f32():
np.cos(arr_f32, out_f32)
#@bench
def numba_cos_f32():
nb_cos(arr_f32, out_f32)
#@bench
def python_cos_f64():
np.cos(arr_f64, out_f64)
#@bench
def numba_cos_f64():
nb_cos(arr_f64, out_f64)
if __name__ == '__main__':
for b in bench._benchmarks:
print(benchmark(b))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment