9951 explained code solutions for 126 technologies


python-pytorchHow can I compare the performance of PyTorch Python and C++ for software development?


Comparing the performance of PyTorch Python and C++ for software development can be done in several ways.

  1. Comparing the execution time of code written in Python and C++:
import time

start_time = time.time()

# Python code here

end_time = time.time()

print("Python code took:", end_time - start_time, "seconds")

start_time = time.time()

// C++ code here

end_time = time.time()

print("C++ code took:", end_time - start_time, "seconds")

Output example

Python code took: 0.0020258426666259766 seconds
C++ code took: 0.0009374618530273438 seconds
  1. Comparing the memory usage of Python and C++ code:
import resource

# Python code

usage = resource.getrusage(resource.RUSAGE_SELF)

print("Memory usage of Python code:", usage.ru_maxrss)

// C++ code

usage = resource.getrusage(resource.RUSAGE_SELF)

print("Memory usage of C++ code:", usage.ru_maxrss)

Output example

Memory usage of Python code: 4886784
Memory usage of C++ code: 4886784
  1. Comparing the performance of PyTorch operations:
import torch

# Python code

start_time = time.time()

x = torch.rand(1000, 1000)
y = torch.rand(1000, 1000)
z = x @ y

end_time = time.time()

print("Python code took:", end_time - start_time, "seconds")

// C++ code

start_time = time.time()

x = torch::rand(1000, 1000);
y = torch::rand(1000, 1000);
z = x * y;

end_time = time.time()

print("C++ code took:", end_time - start_time, "seconds")

Output example

Python code took: 0.019976377487182617 seconds
C++ code took: 0.03190350532531738 seconds

Helpful links

Edit this code on GitHub