当前位置: 首页 > news >正文

硬盘 <-> CPU, CPU <-> GPU 数据传输速度

1. 硬盘 <-> CPU 数据传输速度

import time
import os# 定义文件大小和测试文件路径
file_size = 1 * 1024 * 1024 * 100  # 100 MB 的文件大小
file_path = "test_file.bin"# 创建一个测试文件并测量写入速度
def test_write_speed():data = os.urandom(file_size)  # 生成随机数据start_time = time.time()  # 记录开始时间with open(file_path, 'wb') as f:f.write(data)end_time = time.time()  # 记录结束时间write_time = end_time - start_timewrite_speed = file_size / (write_time * 1024 * 1024)  # 转换为 MB/sprint(f"Disk write speed: {write_speed:.6f} MB/s")# 测试硬盘读取速度
def test_read_speed():start_time = time.time()  # 记录开始时间with open(file_path, 'rb') as f:data = f.read()end_time = time.time()  # 记录结束时间read_time = end_time - start_timeread_speed = file_size / (read_time * 1024 * 1024)  # 转换为 MB/sprint(f"Disk read speed: {read_speed:.6f} MB/s")# 进行测试
test_write_speed()
test_read_speed()# 删除测试文件
os.remove(file_path)

2. CPU <-> GPU 数据传输速度

import torch
import time# 设置设备
device_cpu = torch.device('cpu')
device_gpu = torch.device('cuda' if torch.cuda.is_available() else 'cpu')# 创建一个随机张量在 CPU 上
size = 10**7  # 可以根据需要调整大小,元素数量
data_cpu = torch.randn(size).to(device_cpu)# 计算数据大小,假设是 float32 类型,每个元素 4 字节
data_size_bytes = data_cpu.nelement() * data_cpu.element_size()# 测量 CPU -> GPU 传输时间
start_time = time.time()
data_gpu = data_cpu.to(device_gpu)
torch.cuda.synchronize()  # 确保 GPU 操作完成
cpu_to_gpu_time = time.time() - start_time# 计算传输速度 (MB/s)
cpu_to_gpu_speed = data_size_bytes / (cpu_to_gpu_time * 1024 * 1024)  # 转换为 MB/s
print(f"CPU -> GPU data transfer speed: {cpu_to_gpu_speed:.6f} MB/s")# 测量 GPU -> CPU 传输时间
start_time = time.time()
data_back_to_cpu = data_gpu.to(device_cpu)
torch.cuda.synchronize()  # 确保 GPU 操作完成
gpu_to_cpu_time = time.time() - start_time# 计算传输速度 (MB/s)
gpu_to_cpu_speed = data_size_bytes / (gpu_to_cpu_time * 1024 * 1024)  # 转换为 MB/s
print(f"GPU -> CPU data transfer speed: {gpu_to_cpu_speed:.6f} MB/s")
http://www.lryc.cn/news/477635.html

相关文章:

  • 数据编排与ETL有什么关系?
  • 来了解一下!!!——React
  • 用vite创建项目
  • json-server的使用(根据json数据一键生成接口)
  • 半波正弦信号的FFT变换
  • Python数据分析NumPy和pandas(二十三、数据清洗与预处理之五:pandas的分类类型数据)
  • redis源码系列--(二)--multi/exec/eval命令执行流程
  • 【力扣打卡系列】移动零(双指针)
  • 无源元器件-电容选型参数总结
  • Linux下的socket编程
  • 【算法】Floyd多源最短路径算法
  • iOS SmartCodable 替换 HandyJSON 适配记录
  • 使用 axios 拦截器实现请求和响应的统一处理(附常见面试题)
  • 阿里 Sentinel
  • 【点云网络】 pointnet 和 pointnet++
  • .net core mvc 控制器中页面跳转
  • 大学适合学C语言还是Python?
  • 跳表原理课堂笔记
  • Windows系统使用OpenSSL生成自签名证书
  • 定位new的表达式
  • 矩阵特殊打印方式
  • OCC 拟合的平面转换为有界平面
  • Nginx性能优化的几个方法
  • Unity性能优化5【物理篇】
  • 我的工具列表
  • 985研一学习日记 - 2024.11.5
  • Vue2 与 Vue3 的区别
  • 虚拟现实技术课程开发思路
  • triangle_area_calculators库发布
  • ClickHouse数据库SSL配置和SSL连接测试