Pytorch CPU Tensor与GPU Tensor的运算速度对比测试

分别使用CPU和GPU进行Pytorch中的Tensor(张量)计算,测试Tensor在两种不同运算设备上的计算速度差异。

设备:

服务器:Dell EMC Power Edge R740

CPU:Intel Xeon Gold 5117 * 2

Memory:64G

GPU:NVIDIA Tesla T4 16G * 1

Python Version:3.8

CUDA Version:11.4

Pytorch Version:1.9.0

分别使用三种不同尺寸的Tensor进行平方运算测试,单次测试进行10万次平方运算,分别在CPU和GPU上进行20次测试,时长取20次测试的平均结果。

Code:



import warnings
warnings.filterwarnings('ignore')


import torch
import time


if torch.cuda.is_available():
    device=torch.device('cuda:0')
    print('The current device is GPU. ',end='\n\n')
else:
    device=torch.device('cpu')
    print('The current device is CPU. ',end='\n\n')


a=torch.normal(mean=0, std=1, size=(32,128,128))
    

b=a.clone()
d=a.clone()
d=d.to(device)


Test_times=20


time_cost=0
for _ in range(Test_times):
    time_0=time.time()
    for i in range(100000):
        c=b**2
    time_1=time.time()
    
    time_cost=time_cost + time_1-time_0
time_cost=time_cost/Test_times
    
print(f'Average CPU Time : {time_cost:.5f} ')


time_cost=0
for _ in range(Test_times):
    time_2=time.time()
    for i in range(100000):
        e=d**2
    time_3=time.time()
    
    time_cost=time_cost + time_3-time_2
time_cost=time_cost/Test_times

print(f'Average GPU Time : {time_cost:.5f} ')





Result:

1.    32*64*64

2.    32*128*128

3.    32*256*256

Analysis:

从测试结果来看,无论何种尺寸的张量计算,在GPU上的运算速度都要远快于CPU。

 

 

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Think@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值