In [84]:
%matplotlib inline
# 多行结果输出支持
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
In [14]:
import numpy as np
import numba
numba.__version__
Out[14]:
In [28]:
# 使用装饰器声明
@numba.jit
def sum2d(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
x = np.random.randn(8000, 8000) * 5000 ** 3
sum2d(x)
Out[28]:
In [13]:
# 不使用加速
def sum2d(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
x = np.random.randn(8000, 8000) * 5000 ** 3
sum2d(x)
Out[13]:
In [25]:
@numba.jit(nopython=True)
def f(x, y):
return x + y
f(1, 2)
f(2j, 3)
Out[25]:
Out[25]:
In [24]:
# 超出 32bit的数值将会被丢弃
@numba.jit([numba.complex128(numba.complex128, numba.complex128)],
target='cpu')
def f(x, y):
return x + y
f(1, 2)
# 类型错误
f(2j, 2 + 3j)
Out[24]:
Out[24]:
int8
, uint8
, int16
, uint16
, int32
, uint32
, int64
, uint64
(固定宽度的整型)float32
, float64
(单精度和双精度浮点型)complex64
, complex128
(单精度和双精度复数浮点型)
In [31]:
from numba import jit
jit1 = jit(nopython=True, parallel=True)
In [32]:
@jit1
def f(x, y):
return x + y
In [35]:
from numba import generated_jit, types
@generated_jit(nopython=True)
def is_missing(x):
"""
Return True if the value is missing, False otherwise.
"""
if isinstance(x, types.Float):
return lambda x: np.isnan(x)
elif isinstance(x, (types.NPDatetime, types.NPTimedelta)):
# The corresponding Not-a-Time value
missing = x('NaT')
return lambda x: x == missing
else:
return lambda x: False
# test
is_missing(np.NaN)
Out[35]:
In [58]:
from numba import vectorize
from numba import (int16, int32, int64, float32, float64, complex128)
In [59]:
vec_cpu = vectorize([
int16(int16, int16),
int32(int32, int32),
int64(int64, int64),
float32(float32, float32),
float64(float64, float64),
complex128(complex128, complex128)
])
@vec_cpu
def f(x, y):
return x + y
In [60]:
a = np.arange(6)
a
a + a
f(a, a)
Out[60]:
Out[60]:
Out[60]:
In [61]:
a = np.linspace(0, 1, 6)
a
f(a, a)
Out[61]:
Out[61]:
In [63]:
a = np.linspace(0, 1+1j, 6)
a
# 没有写支持, 把complex128加入就可以了
f(a, a)
Out[63]:
Out[63]:
In [70]:
# from functools import reduce
In [78]:
a = np.arange(12).reshape(3, 4)
a
Out[78]:
In [77]:
# numpy 的reduce
f.reduce(a, axis=0)
Out[77]:
In [73]:
f.reduce(a, axis=1)
Out[73]:
In [75]:
f.accumulate(a)
Out[75]:
In [76]:
f.accumulate(a, axis=1)
Out[76]:
In [81]:
from numba import njit, prange
@njit(parallel=True)
def prange_test(A):
s = 0
for i in prange(A.shape[0]):
s += A[i]
return s
prange_test(np.array([1, 3, 4, 6, 8]))
Out[81]:
In [92]:
from timeit import default_timer as timer
from matplotlib.pylab import imshow, jet, show, ion
import numpy as np
from numba import jit
@jit
def mandel(x, y, max_iters):
"""
Given the real and imaginary parts of a complex number,
determine if it is a candidate for membership in the Mandelbrot
set given a fixed number of iterations.
"""
i = 0
c = complex(x,y)
z = 0.0j
for i in range(max_iters):
z = z*z + c
if (z.real*z.real + z.imag*z.imag) >= 4:
return i
return 255
@jit
def create_fractal(min_x, max_x, min_y, max_y, image, iters):
height = image.shape[0]
width = image.shape[1]
pixel_size_x = (max_x - min_x) / width
pixel_size_y = (max_y - min_y) / height
for x in range(width):
real = min_x + x * pixel_size_x
for y in range(height):
imag = min_y + y * pixel_size_y
color = mandel(real, imag, iters)
image[y, x] = color
return image
image = np.zeros((500 * 2, 750 * 2), dtype=np.uint8)
s = timer()
create_fractal(-2.0, 1.0, -1.0, 1.0, image, 20)
e = timer()
print(e - s)
imshow(image)
jet()
# ion()
show()
Out[92]:
Out[92]:
In [98]:
import numba
@numba.jit()
def c(n):
count=0
for i in range(n):
for i in range(n):
count+=1
return count
n=99999
c(n)
Out[98]:
In [94]:
def c(n):
count=0
for i in range(n):
for i in range(n):
count+=1
return count
n=99999
c(n)
Out[94]:
In [ ]: