Created
January 15, 2025 21:44
-
-
Save strumer69/c4acf68ba9bc5abde5adfffe2e04080d to your computer and use it in GitHub Desktop.
compare the GPU and CPU in matrix multiplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import time | |
def measure_time(device): | |
# create random matrix | |
size=1000 # size of matrix | |
mat1=torch.randn(size,size, device=device) | |
mat2=torch.randn(size,size , device=device) | |
# measure the time taken for matrix multiplication | |
start_time = time.time() | |
result = torch.mm(mat1,mat2) | |
end_time = time.time() | |
return end_time - start_time | |
# run on CPU | |
cpu_time = measure_time("cpu") | |
print(f"Time on CPU:{cpu_time:.6f} second") | |
# print(f"Time on CPU: {cpu_time:.6f} seconds") | |
# check if GPU is available | |
if torch.cuda.is_available(): | |
#Run on GPU | |
gpu_time= measure_time("cuda") | |
print(f"Time on GPU:{gpu_time:.6f}second") | |
else: | |
print("GPU not available") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment