TF2.8 CPU及GPU速度测试

测试CPU及GPU在TF下面的速度差异

import tensorflow as tf
import time

def time_matmul(x):
  start = time.time()
  range_1 = 1000
  for loop in range(range_1):
    tf.matmul(x, x)

  result = time.time()-start

  print("{} loops: {:0.2f}ms".format(range_1,1000*result))
    
print("Normal Running:")
x = tf.random.uniform([1000, 1000])
time_matmul(x)
# Force execution on CPU
print("On CPU:")
with tf.device("CPU:0"):
  x = tf.random.uniform([1000, 1000])
  assert x.device.endswith("CPU:0")
  time_matmul(x)

# Force execution on GPU #0 if available
if tf.config.list_physical_devices("GPU"):
  print("On GPU:")
  with tf.device("GPU:0"): # Or GPU:1 for the 2nd GPU, GPU:2 for the 3rd etc.
    x = tf.random.uniform([1000, 1000])
    assert x.device.endswith("GPU:0")
    time_matmul(x)```

--------
测试结果:
1000 loops: 372.44ms
On CPU:
1000 loops: 5694.99ms
On GPU:
1000 loops: 305.03ms
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值