In [24]:
from numba import jit


@jit
def f(x, y):
    return x + y

In [25]:
%timeit f(1, 2)


The slowest run took 132984.30 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 202 ns per loop

In [8]:
f(1j, 2)


Out[8]:
(2+1j)

In [20]:
from numba import int32

@jit(int32(int32, int32))
def f1(x, y):
    return x + y

In [22]:
%timeit f1(1,2)


The slowest run took 29.61 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 213 ns per loop

In [14]:
f1(1j, 2)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-19300b83c20f> in <module>()
----> 1 f1(1j, 2)

C:\ProgramData\Anaconda3\lib\site-packages\numba\dispatcher.py in _explain_matching_error(self, *args, **kws)
    397         msg = ("No matching definition for argument type(s) %s"
    398                % ', '.join(map(str, args)))
--> 399         raise TypeError(msg)
    400 
    401     def _search_new_conversions(self, *args, **kws):

TypeError: No matching definition for argument type(s) complex128, int64

In [23]:
%timeit f(2**32, 2**31 + 1)


The slowest run took 12.92 times longer than the fastest. This could mean that an intermediate result is being cached.
1000000 loops, best of 3: 275 ns per loop

In [26]:
!ping www.bbc.co.uk


正在 Ping www.bbc.net.uk [212.58.246.93] 具有 32 字节的数据:
来自 212.58.246.93 的回复: 字节=32 时间=280ms TTL=46
来自 212.58.246.93 的回复: 字节=32 时间=318ms TTL=46
来自 212.58.246.93 的回复: 字节=32 时间=311ms TTL=46
来自 212.58.246.93 的回复: 字节=32 时间=300ms TTL=46

212.58.246.93 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 280ms,最长 = 318ms,平均 = 302ms

In [27]:
from IPython.display import display

In [28]:
display(print)


<function print>

In [31]:
import numpy as np
from numba import generated_jit, types

@ generated_jit(nopython=True)
def is_missing(x):
    if isinstance(x, types.Float):
        return lambda x: np.isnan(x)
    elif isinstance(x, types.NPDatetime, types.NPTimedelta):
        missing = x(NaT)
        return lambda x: x == missing
    else:
        return lambda: False

In [34]:
is_missing(np.NaN)


Out[34]:
True

In [36]:
from numba import vectorize, float64

@vectorize([float64(float64, float64)])
def f3(x, y):
    return x + y

In [38]:
f3(2.0, 3.33)


Out[38]:
5.3300000000000001

In [42]:
from numba import float64, int64, float32

@vectorize([int32(int32, int32),
            int64(int64, int64),
            float32(float32, float32),
            float64(float64, float64)])
def f(x, y):
    return x + y

In [48]:
a = np.arange(6,dtype='float64')
a


Out[48]:
array([ 0.,  1.,  2.,  3.,  4.,  5.])

In [49]:
f(a,a)


Out[49]:
array([  0.,   2.,   4.,   6.,   8.,  10.])

In [50]:
a = np.linspace(0, 1+1j, 6)
a


Out[50]:
array([ 0.0+0.j ,  0.2+0.2j,  0.4+0.4j,  0.6+0.6j,  0.8+0.8j,  1.0+1.j ])

In [51]:
f(a,a)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-51-12d30bce134a> in <module>()
----> 1 f(a,a)

TypeError: ufunc 'f' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

In [52]:
a = np.arange(12).reshape(3,4)

In [53]:
a


Out[53]:
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

In [54]:
f(a, a)


Out[54]:
array([[ 0,  2,  4,  6],
       [ 8, 10, 12, 14],
       [16, 18, 20, 22]])

In [55]:
f.reduce(a, axis=0)  # 降维


Out[55]:
array([12, 15, 18, 21])

In [56]:
f.reduce(a, axis=1)


Out[56]:
array([ 6, 22, 38])

In [58]:
f.accumulate(a, axis=1)


Out[58]:
array([[ 0,  1,  3,  6],
       [ 4,  9, 15, 22],
       [ 8, 17, 27, 38]])

In [59]:
f.accumulate(a)


Out[59]:
array([[ 0,  1,  2,  3],
       [ 4,  6,  8, 10],
       [12, 15, 18, 21]])

In [63]:
from numba import guvectorize

@guvectorize([(int64[:], int64[:], int64[:])], '(n),()->(n)', nopython=True)
def g(x, y, res):
    for i in range(x.shape[0]):
        res[i] = x[i] + y[0]

In [65]:
a = np.arange(6).reshape(2, 3)
g(a, 10)


Out[65]:
array([[10, 11, 12],
       [13, 14, 15]], dtype=int64)

In [66]:
from numba import cfunc

@cfunc("float64(float64, float64)")
def add(x, y):
    return x + y

In [67]:
@cfunc("float64(float64, float64)")
def add(x, y):
    return x + y

print(add.ctypes(4.0, 5.0))  # prints "9.0"


9.0

In [68]:
from numba.pycc import CC

cc = CC('my_module')
# Uncomment the following line to print out the compilation steps
#cc.verbose = True

@cc.export('multf', 'f8(f8, f8)')
@cc.export('multi', 'i4(i4, i4)')
def mult(a, b):
    return a * b

@cc.export('square', 'f8(f8)')
def square(a):
    return a ** 2
cc.compile()

In [69]:
import my_module
my_module.multi(3, 4)


Out[69]:
12

In [70]:
my_module.square(1.414)


Out[70]:
1.9993959999999997

In [72]:
f.inspect_types()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-72-d5cd027f6f78> in <module>()
----> 1 f.inspect_types()

AttributeError: 'DUFunc' object has no attribute 'inspect_types'

In [ ]: