44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
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")
|