2024-10-27 02:13:51 +04:00
|
|
|
import time
|
|
|
|
import random
|
|
|
|
from matrix_multiplication.sequential import matrix_multiply_sequential
|
|
|
|
from matrix_multiplication.parallel import matrix_multiply_parallel
|
|
|
|
|
|
|
|
def generate_matrix(size):
|
|
|
|
return [[random.randint(0, 10) for _ in range(size)] for _ in range(size)]
|
|
|
|
|
|
|
|
def benchmark(matrix_size, num_threads):
|
|
|
|
A = generate_matrix(matrix_size)
|
|
|
|
B = generate_matrix(matrix_size)
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
matrix_multiply_sequential(A, B)
|
|
|
|
sequential_time = time.time() - start
|
|
|
|
|
|
|
|
start = time.time()
|
|
|
|
matrix_multiply_parallel(A, B, num_threads)
|
|
|
|
parallel_time = time.time() - start
|
|
|
|
|
|
|
|
print(f"Размер матрицы: {matrix_size}x{matrix_size}")
|
|
|
|
print(f"Последовательное время: {sequential_time:.5f} сек")
|
|
|
|
print(f"Параллельное время ({num_threads} потоков): {parallel_time:.5f} сек")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
for size in [100, 300, 500]:
|
2024-10-27 02:16:28 +04:00
|
|
|
benchmark(size, num_threads=4)
|