77 lines
2.6 KiB
Python
77 lines
2.6 KiB
Python
import time
|
||
import numpy as np
|
||
import concurrent.futures
|
||
import psutil
|
||
|
||
|
||
def calculate_determinant(args):
|
||
matrix, i = args
|
||
multiplier = matrix[0][i]
|
||
if i % 2 != 0:
|
||
multiplier *= -1
|
||
matrix = np.delete(matrix, 0, axis=0)
|
||
submatrix = np.delete(matrix, i, axis=1)
|
||
return np.linalg.det(submatrix) * multiplier
|
||
|
||
|
||
def parallel_determinant(matrix, parallel):
|
||
memory = psutil.virtual_memory()
|
||
n = matrix.shape[0]
|
||
if parallel:
|
||
with concurrent.futures.ThreadPoolExecutor(max_workers=n) as executor:
|
||
results = []
|
||
start_time = time.time()
|
||
for i in range(n):
|
||
results.append(executor.submit(calculate_determinant, args=(matrix, i)))
|
||
result = np.sum([res.result() for res in results])
|
||
end_time = time.time()
|
||
else:
|
||
start_time = time.time()
|
||
result = np.linalg.det(matrix)
|
||
end_time = time.time()
|
||
execution_time = end_time - start_time
|
||
return result, execution_time, psutil.cpu_percent(interval=1), memory.percent
|
||
|
||
|
||
def test(parallel):
|
||
mx = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
|
||
result = parallel_determinant(mx, parallel)
|
||
print(f"Определитель матрицы: {result[0]}")
|
||
print(f"Время выполнения: {result[1]} сек.")
|
||
print(f"Загрузка ЦП: {result[2]} %")
|
||
print(f"Использование ОЗУ: {result[3]} %")
|
||
|
||
|
||
def bench50x50(parallel):
|
||
mx = np.random.randint(0, 100, size=(50, 50))
|
||
result = parallel_determinant(mx, parallel)
|
||
print(f"Определитель матрицы: {result[0]}")
|
||
print(f"Время выполнения: {result[1]} сек.")
|
||
print(f"Загрузка ЦП: {result[2]} %")
|
||
print(f"Использование ОЗУ: {result[3]} %")
|
||
|
||
|
||
def bench75x75(parallel):
|
||
mx = np.random.randint(0, 100, size=(75, 75))
|
||
result = parallel_determinant(mx, parallel)
|
||
print(f"Определитель матрицы: {result[0]}")
|
||
print(f"Время выполнения: {result[1]} сек.")
|
||
print(f"Загрузка ЦП: {result[2]} %")
|
||
print(f"Использование ОЗУ: {result[3]} %")
|
||
|
||
|
||
def bench125x125(parallel):
|
||
mx = np.random.randint(0, 100, size=(125, 125))
|
||
result = parallel_determinant(mx, parallel)
|
||
print(f"Определитель матрицы: {result[0]}")
|
||
print(f"Время выполнения: {result[1]} сек.")
|
||
print(f"Загрузка ЦП: {result[2]} %")
|
||
print(f"Использование ОЗУ: {result[3]} %")
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# test(parallel=False)
|
||
# bench50x50(parallel=False)
|
||
# bench75x75(parallel=False)
|
||
bench125x125(parallel=False)
|