TensorFlow GPU and CPU running speed comparison

Test TensorFlow using GPU or CPU to test the running speed of the same task.

I am using TensorFlow for the first time and python in the second month. I am not in the CS industry.

  • test environment

Software environment: windows10, VScode, TensorFlow2.x (actually the code is tf1.x version)

Hardware environment: E5-2667v3QS×2 compared to 2080Ti (Colorful OC), 112G memory

  • test code

Original code comes from: https://blog.csdn.net/beyond9305/article/details/90450246

Parameter 0 of the argv tuple means using GPU or CPU for calculation; parameter 1 of the argv tuple is the calculation scale. It is recommended that the maximum value does not exceed 30,000, otherwise the memory or video memory may be exhausted.

import sys
# 以下两行代码以使用tf1.x版本
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
# 需要datetime
from datetime import datetime
 
argv = ("gpu",30000) 
device_name = argv[0]  # Choose device from cmd line. Options: gpu or cpu
# device_name = "gpu"  
shape = (int(argv[1]), int(argv[1]))
if device_name == "gpu":
    device_name = "/gpu:0"
else:
    device_name = "/cpu:0"
 
with tf.device(device_name):
    random_matrix = tf.random_uniform(shape=shape, minval=0, maxval=1)
    dot_operation = tf.matmul(random_matrix, tf.transpose(random_matrix))
    sum_operation = tf.reduce_sum(dot_operation)
 
startTime = datetime.now()
with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as session:
        result = session.run(sum_operation)
        print(result)
 
# It can be hard to see the results on the terminal with lots of output -- add some newlines to improve readability.
print("\n" * 5)
print("Shape:", shape, "Device:", device_name)
print("Time taken:", datetime.now() - startTime)
print("\n" * 5)
# 运行结果:
# CPU:
#  Shape: (30000, 30000) Device: /cpu:0
#  Time taken: 0:00:55.894182
# GPU:
#  Shape: (30000, 30000) Device: /gpu:0
#  Time taken: 0:00:07.981562

The above code has a very high utilization rate for multi-core CPUs, with 16 cores and 32 threads approaching 100% usage.

  • Test Results

Dual-channel E5 2667v3 (2.9GHz, total 16 cores and 32 threads), running time 55.89s

Colorful 2080Ti OC running time 7.98s

 

Guess you like

Origin blog.csdn.net/wenquantongxin/article/details/107215106