numba加速python程序
2021/8/11 22:06:36
本文主要是介绍numba加速python程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
numba
numba加速循环、numpy的一些运算,大概是将python和numpy的一些代码转化为机器代码,速度飞快!
加速耗时很长的循环时:
from numba import jit # 在函数前加 @jit(nopython=True) def func(): ...
但是条件比较苛刻,比如函数内不能对全局变量进行修改,不能有未明确类型的list和dict(否则需要固定类型,在初始化的时候用 typed.List.empty_list(types.float64)
这种)
举个例子:
@jit(nopython=True) def func(arr, times): # 这里 arr 和 times 可以是numpy数组。函数内不能对外部的变量进行修改 for i in range(character_num): s = 0 for j in range(character_num): s += arr[i][j] for j in range(character_num): if arr[i][j] != 0: arr[i][j] = arr[i][j] / s arr[i][j] = lmbda * arr[i][j] + (1 - lmbda) * times[j] return arr
若要对list, dict等类型进行操作,使用类似于typed.List.empty_list(types.float64)
的操作来初始化
文档可见https://numba.readthedocs.io/en/stable/index.html
这篇关于numba加速python程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python