We all know that Python runs very slowly, but more wheels, so the user is very broad, in a variety of fields can be used on Python, but the most headache is still, to solve the problem of running speed, so here to introduce you to numba, is basically equal to the reengineering language. But not many numpy functions are supported. To make the function that can jit up more to do. The following is a detailed introduction to the use of numba to improve python running speed method.
numba profile:
Ability to implement compilation of python functions to machine code for faster operation.
Working role:
Give python a different compiler.
Use numba:
1. Import numba and its compiler
import numpy as np import numba from numba import jit
2, pass numba decorator jit, write function
@jit(nopython=True) def go_fast(a): trace = 0 for i in range([0]): trace += (a[i, i]) return a + trace
3, function pass real parameters
x = (100).reshape(10, 10) go_fast(x)
4. Accelerated function execution time
% timeit go_fast(x)
Knowledge Point Expansion:
numba is good for scientific computing
numpy is designed for computational tasks oriented to numpy arrays.
In array-oriented computational tasks, data parallelism is natural for gas pedals like GPUs.Numba understands NumPy array types and uses them to generate efficient compiled code for execution on GPUs or multicore CPUs. Special decorators can also create functions that broadcast on numpy arrays like numpy functions.
When is numba used?
- When doing a lot of scientific calculations with numpy arrays
- When using a for loop
to this article on numba to enhance the speed of python running example of the method of the article is introduced to this, more related to how to use numba to enhance the speed of python running content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!