python代码由cpu转成gpu运行
程序员文章站
2022-07-13 15:02:33
...
参考文件章1:https://blog.csdn.net/qq_28023365/article/details/87970505
参考文件章2:https://blog.csdn.net/u014636245/article/details/84404646
看起来是通过Numba模块,通过装饰器@jit实现转换
有一块样例代码:
import numpy as np
from numba import jit
import time
@jit
def sum_jit(arr):
s_time = time.time()
m = arr.shape[0]
result = 0.0
for i in range(m):
result += arr[i]
e_time = time.time()
return (e_time - s_time)
def sum(arr):
s_time = time.time()
m = arr.shape[0]
result = 0.0
for i in range(m):
result += arr[i]
e_time = time.time()
return (e_time-s_time)
def main():
n = int(10.0*1e6)
array = np.random.random(n)
t1 = sum_jit(array)
t2 = sum(array)
print("Time with JIT:", t1)
print("Time without JIT:", t2)
if __name__ == '__main__':
main()
运行后的截图如下:
运行前面报了一些错,可能是我机器上是集成显卡的缘故吧,后面还要再细看
上一篇: 设计模式-策略模式
下一篇: Unity简易对象池管理器
推荐阅读