Commit 3f5d20df by mozheng

不符合题意,更改

parent 98c4f409
from numba import jit
from numpy import arange
import cupy as cp
import numpy as np
import time
@jit
def numba_sum(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
def sum(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
def matrix_add(matrix, a, b, times):
#矩阵的相加
for _ in range(times):
c = matrix.add(a,b)
return c
if __name__ == "__main__":
a = arange(9).reshape(3,3)
h, w = 10000, 10000
a = np.random.normal(size=h*w)
a = a.reshape((h,w))
b = np.random.normal(size=h*w)
b = b.reshape((h,w))
start_time = time.time()
for i in range(10000000):
numba_sum(a)
matrix_add(np, a, b, 100)
end_time = time.time()
print ("使用numba:", end_time - start_time)
print("使用numpy(cpu): {0} sec".format(end_time-start_time))
a = cp.random.normal(size=h*w)
a = a.reshape((h,w))
b = cp.random.normal(size=h*w)
b = b.reshape((h,w))
start_time = time.time()
for i in range(10000000):
sum(a)
matrix_add(cp, a, b, 100)
end_time = time.time()
print ("使用cpu:", end_time - start_time)
\ No newline at end of file
print("使用cupy(gpu): {0} sec".format(end_time-start_time))
......@@ -6,4 +6,4 @@ Cupy是使用NVIDIA CUDA核心进行加速运算,因为GPU上有好多CUDA核
代码:[main.py](main.py)
结果:
![](res1.png)
\ No newline at end of file
![](res1.jpg)
\ No newline at end of file
http://47.94.6.102/113/mozheng-homework
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment