惊呆了!这样可以将Numpy加速700倍!
Python 知识大全
共 2502字,需浏览 6分钟
·
2020-09-26 02:13
号外: 本号免费提供 CSDN 资源下载,需要的伙伴公众号后台回复【CSDN】
选自towardsdatascience, 作者:George Seif 本文转自机器之心(nearhuman2014)
pip install cupy
i7–8700k CPU
1080 Ti GPU
32 GB of DDR4 3000MHz RAM
CUDA 9.0
import numpy as np
import cupy as cp
import time
### Numpy and CPU
s = time.time()
*x_cpu = np.ones((1000,1000,1000))*
e = time.time()
print(e - s)### CuPy and GPU
s = time.time()
*x_gpu = cp.ones((1000,1000,1000))*
e = time.time()
print(e - s)
### Numpy and CPU
s = time.time()
*x_cpu *= 5*
e = time.time()
print(e - s)### CuPy and GPU
s = time.time()
*x_gpu *= 5*
e = time.time()
print(e - s)
数组乘以 5
数组本身相乘
数组添加到其自身
### Numpy and CPU
s = time.time()
*x_cpu *= 5
x_cpu *= x_cpu
x_cpu += x_cpu*
e = time.time()
print(e - s)### CuPy and GPU
s = time.time()
*x_gpu *= 5
x_gpu *= x_gpu
x_gpu += x_gpu*
e = time.time()
print(e - s)
推荐阅读 关于 Python 3.9,那些你不知道的事 总结了 90 条写 Python 程序的建议
关注「Python 知识大全」,做全栈开发工程师 岁月有你 惜惜相处
评论