DAS_2023_1/antonov_dmitry_lab_5/benchmark.py
DmitriyAntonov f279209740 good start
2023-12-04 21:25:59 +04:00

44 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import numpy as np
import time
import concurrent.futures
def multiply_matrices(matrix_a, matrix_b):
return np.dot(matrix_a, matrix_b)
def multiply_matrices_parallel(matrix_a, matrix_b):
with concurrent.futures.ThreadPoolExecutor() as executor:
result = executor.submit(np.dot, matrix_a, matrix_b)
return result.result()
def benchmark_sequential(size):
matrix_a = np.random.rand(size, size)
matrix_b = np.random.rand(size, size)
start_time = time.time()
multiply_matrices(matrix_a, matrix_b)
end_time = time.time()
return end_time - start_time
def benchmark_parallel(size):
matrix_a = np.random.rand(size, size)
matrix_b = np.random.rand(size, size)
start_time = time.time()
multiply_matrices_parallel(matrix_a, matrix_b)
end_time = time.time()
return end_time - start_time
if __name__ == "__main__":
sizes = [100, 300, 500, 700, 900, 1000, 1200, 1400, 1700, 2000]
for size in sizes:
sequential_time = benchmark_sequential(size)
parallel_time = benchmark_parallel(size)
print(f"Размер матрицы: {size}x{size}")
print(f"Время обычное: {sequential_time} с")
print(f"Время параллельное: {parallel_time} с")
print(f"Ускорение: {sequential_time / parallel_time}\n")