1. 程式人生 > >python加速模組numba的用法

python加速模組numba的用法

--------------程式碼部分------------
import numba as nb
import time

# 普通的 for
def add1(t):
    start = time.time()
    s=0
    for i in range(t):
        s+=i
    onlytime=time.time()-start
    return  onlytime

# pthon 內建函式
def add2(t):
    start = time.time()
    s=sum(range(1, t))
    onlytime = time.time() - start
    return  onlytime

# 使用 jit 加速後的 for
@nb.jit()
def add_with_jit(t):
    start = time.time()
    s = sum(range(1, t))
    onlytime = time.time() - start
    return onlytime

@nb.jit()
def add_with_jitf(t):
    start = time.time()
    s = 0
    for i in range(t):
        s += i
    onlytime = time.time() - start
    return onlytime

print("普通的for迴圈{}".format(add1(100000000)))                  時間: 5.89318323135376
print("普通的for迴圈+jit{}".format(add_with_jitf(100000000)))     時間: 0.0381016731262207
print("python內建函式{}".format(add2(100000000)))                 時間: 4.128971099853516
print("python內建函式+jit{}".format(add_with_jit(100000000)))     時間: 4.199178218841553

————————————————————————————————————

普通的for迴圈5.89318323135376 普通的for迴圈+jit0.0381016731262207 python內建4.128971099853516 python內建+jit4.199178218841553

[email protected]() 用法和引數-------------------------

官方原始碼  個人理解如下

#這個裝飾器的作用是將python函式編譯成機器語言進行運算

signature 可以是一個函式或者一個列表 @jit("int32(int32, int32)")   @jit(["int32(int32, int32)", "float32(float32, float32)"])

locals  可以是個字典對映成numba

target  numba利用那個平臺進行編譯,cpu, gpu, npyufunc, cuda,預設cpu

pipeline_class   通過管道的方式進行編譯,個人理解就是把引數集合到一起

--------------------------------官方程式碼------------------

def jit(signature_or_function=None, locals={}, target='cpu', cache=False,
        pipeline_class=None, **options):
    """
    This decorator is used to compile a Python function into native code.

    Args
    -----
    signature:
        The (optional) signature or list of signatures to be compiled.
        If not passed, required signatures will be compiled when the
        decorated function is called, depending on the argument values.
        As a convenience, you can directly pass the function to be compiled
        instead.

    locals: dict
        Mapping of local variable names to Numba types. Used to override the
        types deduced by Numba's type inference engine.

    target: str
        Specifies the target platform to compile for. Valid targets are cpu,
        gpu, npyufunc, and cuda. Defaults to cpu.

    pipeline_class: type numba.compiler.BasePipeline
            The compiler pipeline type for customizing the compilation stages.

    options:
        For a cpu target, valid options are:
            nopython: bool
                Set to True to disable the use of PyObjects and Python API
                calls. The default behavior is to allow the use of PyObjects
                and Python API. Default value is False.

            forceobj: bool
                Set to True to force the use of PyObjects for every value.
                Default value is False.

            looplift: bool
                Set to True to enable jitting loops in nopython mode while
                leaving surrounding code in object mode. This allows functions
                to allocate NumPy arrays and use Python objects, while the
                tight loops in the function can still be compiled in nopython
                mode. Any arrays that the tight loop uses should be created
                before the loop is entered. Default value is True.

            error_model: str
                The error-model affects divide-by-zero behavior.
                Valid values are 'python' and 'numpy'. The 'python' model
                raises exception.  The 'numpy' model sets the result to
                *+/-inf* or *nan*. Default value is 'python'.

    Returns
    --------
    A callable usable as a compiled function.  Actual compiling will be
    done lazily if no explicit signatures are passed.

    Examples
    --------
    The function can be used in the following ways:

    1) jit(signatures, target='cpu', **targetoptions) -> jit(function)

        Equivalent to:

            d = dispatcher(function, targetoptions)
            for signature in signatures:
                d.compile(signature)

        Create a dispatcher object for a python function.  Then, compile
        the function with the given signature(s).

        Example:

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

            @jit(["int32(int32, int32)", "float32(float32, float32)"])
            def bar(x, y):
                return x + y

    2) jit(function, target='cpu', **targetoptions) -> dispatcher

        Create a dispatcher function object that specializes at call site.

        Examples:

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

            @jit(target='cpu', nopython=True)
            def bar(x, y):
                return x + y

    """
    if 'argtypes' in options:
        raise DeprecationError(_msg_deprecated_signature_arg.format('argtypes'))
    if 'restype' in options:
        raise DeprecationError(_msg_deprecated_signature_arg.format('restype'))

    if options.get('parallel'):
        uns1 = sys.platform.startswith('win32') and sys.version_info[:2] == (2, 7)
        uns2 = sys.maxsize <= 2 ** 32
        if uns1 or uns2:
            msg = ("The 'parallel' target is not currently supported on "
                   "Windows operating systems when using Python 2.7, or "
                   "on 32 bit hardware.")
            raise RuntimeError(msg)
        if cache:
            msg = ("Caching is not available when the 'parallel' target is in "
                   "use. Caching is now being disabled to allow execution to "
                   "continue.")
            warnings.warn(msg, RuntimeWarning)
            cache = False

    # Handle signature
    if signature_or_function is None:
        # No signature, no function
        pyfunc = None
        sigs = None
    elif isinstance(signature_or_function, list):
        # A list of signatures is passed
        pyfunc = None
        sigs = signature_or_function
    elif sigutils.is_signature(signature_or_function):
        # A single signature is passed
        pyfunc = None
        sigs = [signature_or_function]
    else:
        # A function is passed
        pyfunc = signature_or_function
        sigs = None

    dispatcher_args = {}
    if pipeline_class is not None:
        dispatcher_args['pipeline_class'] = pipeline_class
    wrapper = _jit(sigs, locals=locals, target=target, cache=cache,
                   targetoptions=options, **dispatcher_args)
    if pyfunc is not None:
        return wrapper(pyfunc)
    else:
        return wrapper