【发布时间】:2019-11-25 08:57:49
【问题描述】:
我正在研究 numpy 和 cupy 之间的差异,并注意到在我创建的这两个类似程序中,cupy 版本虽然运行在 GPU 上,但速度要慢得多。
这里是 numpy 版本:
import time
import numpy as np
size = 5000
upperBound = 20
dataSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
dataLength = np.random.randint(0, high=upperBound, size=size, dtype='l')
randomNumber = np.random.randint(0, high=62, size=size * upperBound, dtype='l')
count = 0
dataCount = 0
start_time = time.time()
for i in range(size):
lineData = ""
for j in range(dataLength[i]):
lineData = lineData + dataSet[randomNumber[count]]
count = count + 1
print(lineData)
dataCount = dataCount + 1
time = str(time.time() - start_time)
print("------------------------\n" + "It took this many sedonds: " + time)
print("There were " + str(dataCount) + " many data generations.")
这里是cupy版本:
import time
import cupy as cp
size = 5000
upperBound = 20
dataSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
dataLength = cp.random.randint(0, high=upperBound, size= size,dtype='l')
randomNumber = cp.random.randint(0, high=62, size= upperBound * size,dtype='l')
count = 0
dataCount = 0
start_time = time.time()
for i in range(size):
lineData = ""
for j in range(int(dataLength[i])):
lineData = lineData + str(dataSet[int(randomNumber[count])])
count = count + 1
print(lineData)
dataCount = dataCount + 1
time = str(time.time() - start_time)
print("-------------------\n" +"It took this many seconds: " + time)
print("There were " + str(dataCount) + " many data generations.")
它们本质上是相同的代码,除了一个使用numpy而另一个使用cupy。由于 GPU 的使用,我期望 cupy 执行得更快,但事实并非如此。 numpy 的运行时间为:0.032。而 cupy 的运行时间是:0.484。
【问题讨论】:
-
从答案中澄清,此代码在 GPU 上所做的唯一工作是创建随机整数。其他一切都在 CPU 上,有许多小操作,只需将数据从 GPU 复制到 CPU。