Евгений Сергеев 4c907b0556 done!
2024-01-22 02:23:54 +04:00

59 lines
1.9 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
from concurrent.futures import ThreadPoolExecutor
def sequential_determinant(matrix):
return np.linalg.det(matrix)
def parallel_determinant(matrix, num_threads):
determinant = 1.0
n = len(matrix)
def calculate_partial_determinant(start, end):
nonlocal determinant
for i in range(start, end):
determinant *= matrix[i, i]
with ThreadPoolExecutor(max_workers=num_threads) as executor:
chunk_size = n // num_threads
futures = []
for i in range(0, n, chunk_size):
start = i
end = min(i + chunk_size, n)
futures.append(executor.submit(calculate_partial_determinant, start, end))
# Wait for all threads to finish
for future in futures:
future.result()
return determinant
def benchmark(matrix_size, num_threads=1):
matrix = np.random.rand(matrix_size, matrix_size)
start_time = time.time()
sequential_result = sequential_determinant(matrix)
sequential_time = time.time() - start_time
start_time = time.time()
parallel_result = parallel_determinant(matrix, num_threads)
parallel_time = time.time() - start_time
return sequential_time, parallel_time
# Пример использования для матриц размером 100x100, 300x300, 500x500 элементов
matrix_sizes = [100, 300, 500]
num_threads = 1 # Указать желаемое количество потоков
for size in matrix_sizes:
sequential_time, parallel_time = benchmark(size, num_threads)
print(f"Размер матрицы: {size}x{size}")
print(f"Время с последовательным выполнением: {sequential_time:.6f} секунд")
print(f"Время с параллельной обработкой ({num_threads} потоков): {parallel_time:.6f} секунд")
print("=" * 30)