Home > Backend Development > Python Tutorial > How to use Numba to speed up numerical calculations in Python programs

How to use Numba to speed up numerical calculations in Python programs

PHPz
Release: 2023-08-02 17:37:58
Original
1475 people have browsed it

How to use Numba to speed up numerical calculations of Python programs

Introduction:
Python is a very flexible and easy-to-use language when it comes to numerical calculations. However, since Python is an interpreted language, it runs relatively slowly, especially in intensive numerical computing tasks. In order to improve the performance of Python programs, we can use some optimization tools and libraries. One very powerful library is Numba, which uses just-in-time compilation to speed up numerical calculations without changing the structure of Python code. This article will introduce how to use Numba to speed up the numerical calculation of Python programs.

  1. Installing Numba:
    To start using Numba, you first need to install it. Numba can be installed by using the pip package manager:

    pip install numba
    Copy after login
  2. Basic usage:
    The simplest way to use Numba is to use a decorator to apply it to the function that needs to be accelerated. Numba supports two main decorators: @jit and @njit. @jitDecorators can be applied to functions, compiling them into machine code to improve performance. @njitThe decorator is a shortcut to @jit(nopython=True), which converts a function into pure machine code without using the Python interpreter. Here is a simple example:

    from numba import jit
    
    @jit
    def sum_array(arr):
     total = 0
     for i in range(len(arr)):
         total += arr[i]
     return total
    
    arr = [1, 2, 3, 4, 5]
    result = sum_array(arr)
    print(result)
    Copy after login

In the above example, the sum_array function is optimized using the @jit decorator. Numba automatically infers the types of variables in functions and compiles them into machine code. In this way, the performance of the function will be greatly improved.

  1. Type inference and type annotations:
    To maximize performance, Numba needs to know exactly the types of functions and variables. In the above example, Numba can correctly infer the type of the sum_array function. However, in some cases, Numba may not be able to automatically infer the type. In this case, we need to use type annotations to help Numba compile the function accurately. The following is an example of using type annotations:

    from numba import jit
    
    @jit('float64(float64[:])')
    def sum_array(arr):
     total = 0
     for i in range(len(arr)):
         total += arr[i]
     return total
    
    arr = [1.0, 2.0, 3.0, 4.0, 5.0]
    result = sum_array(arr)
    print(result)
    Copy after login

In the above example, we pass @jit('float64(float64[:])') annotation Explicitly tell Numbasum_array the input and output types of the function. This way, Numba can better optimize functions.

  1. Parallel computing:
    Numba also supports parallel computing and can use multi-core CPUs to improve computing performance. To use parallel computing, you need to set the parallel parameter of the @jit decorator to True:

    from numba import njit
    
    @njit(parallel=True)
    def parallel_sum(arr):
     total = 0
     for i in range(len(arr)):
         total += arr[i]
     return total
    
    arr = [1, 2, 3, 4, 5]
    result = parallel_sum(arr)
    print(result)
    Copy after login

In the above example, # The ##parallel_sum function implements parallel computing by applying @njit(parallel=True) to the function. This allows multiple CPU cores to be utilized simultaneously to accelerate calculations.

  1. Code generated using Numba compilation:

    Sometimes we may want to view the machine code generated by Numba compilation. The LLVM code and assembly code generated by Numba can be viewed through the
    inspect_llvm and inspect_asm functions:

    from numba import jit, inspect_llvm, inspect_asm
    
    @jit
    def sum_array(arr):
     total = 0
     for i in range(len(arr)):
         total += arr[i]
     return total
    
    arr = [1, 2, 3, 4, 5]
    result = sum_array(arr)
    
    print(inspect_llvm(sum_array))  # 查看LLVM代码
    print(inspect_asm(sum_array))  # 查看汇编代码
    Copy after login
In the above example, we use

inspect_llvm and inspect_asm functions to view the LLVM code and assembly code of the sum_array function.

Conclusion:

Using Numba can significantly improve the numerical computing performance of Python programs. By simply adding a decorator to the function that needs to be accelerated, we can take advantage of Numba's just-in-time compilation feature to compile Python code into efficient machine code. In addition, Numba also supports type inference, type annotations and parallel computing, providing more optimization options. By using Numba, we can better take advantage of Python's simplicity and flexibility while achieving near-native programming language performance.

References:

    https://numba.pydata.org/
  1. https://numba.pydata.org/numba-doc/latest/ user/jit.html
  2. https://numba.pydata.org/numba-doc/latest/user/examples.html

The above is the detailed content of How to use Numba to speed up numerical calculations in Python programs. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template