GPU加速与非加速的深度学习张量计算对比Demo,使用PyTorch展示关键差异

import torch

import time

创建大型随机张量 (10000x10000)

tensor_size = 10000

x_cpu = torch.randn(tensor_size, tensor_size)

x_gpu = x_cpu.cuda() # 转移到GPU

CPU矩阵乘法

start = time.time()

result_cpu = torch.mm(x_cpu, x_cpu.t())

cpu_time = time.time() - start

GPU矩阵乘法

torch.cuda.synchronize() # 确保GPU计时准确

start = time.time()

result_gpu = torch.mm(x_gpu, x_gpu.t())

torch.cuda.synchronize()

gpu_time = time.time() - start

print(f"CPU计算时间: {cpu_time:.4f}秒")

print(f"GPU计算时间: {gpu_time:.4f}秒")

print(f"加速比: {cpu_time/gpu_time:.1f}倍")

相关推荐
智算菩萨1 分钟前
GPT-5.4辅助科技论文写作完全指南(国内可用)
人工智能·科技·gpt·ai·ai写作·论文写作·ai-native
AiTop1001 分钟前
Cursor承认Composer 2核心基座源自国产大模型Kimi,双方已达成授权合作
人工智能·gpt·ai·aigc·php·composer
专利观察员1 分钟前
专利检索万字报告分享:《专利数据库3.0时代:2021-2025专利数据库的AI浪潮与选型逻辑重构》
数据库·人工智能·科技·专利检索·专利数据库
lovingsoft2 分钟前
vscode 这几个文件夹是做什么用的。
人工智能
进击的阿三姐3 分钟前
AI工作流:用 Claude + Obsidian 打造全自动技术笔记系统
人工智能·ai
早睡早起好好code3 分钟前
InternNav 论文回看
笔记·python·深度学习·学习·算法
Elastic 中国社区官方博客5 分钟前
使用 Elasticsearch Inference API 结合 Hugging Face 模型
大数据·人工智能·elasticsearch·搜索引擎·ai·全文检索
九硕智慧建筑一体化厂家5 分钟前
能碳 IBMS 集成平台:打破数据孤岛,实现建筑全维度智能管控
人工智能
badhope7 分钟前
Matplotlib实战30例:全类型图表代码库
人工智能·python·plotly·github·matplotlib
Tigerbot10 分钟前
2026年3月五家头部GEO服务优化商推荐 重构AI时代的品牌护城河!
人工智能·重构