-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumpy_side.py
49 lines (38 loc) · 1.18 KB
/
numpy_side.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from memory_profiler import memory_usage
import numpy as np
import timeit
import gc
matrix_sizes = [10, 100, 500, 1000, 1500, 2000]
def solve_equation(A, B):
return np.linalg.solve(A, B)
results = []
for matrix_size in matrix_sizes:
# Load matrices
matrix_A = np.loadtxt(f"matrix_A_{matrix_size}.txt")
vector_B = np.loadtxt(f"vector_B_{matrix_size}.txt")
# Time the function using timeit
elapsed_time = timeit.timeit(
"solve_equation(matrix_A, vector_B)",
setup=f"from __main__ import solve_equation, matrix_A, vector_B",
number=1, # Number of executions
)
# Measure memory
mem_usage_before = memory_usage(max_usage=True)
mem_usage, vector_x = memory_usage(
(solve_equation, (matrix_A, vector_B)), max_usage=True, retval=True
)
mem_usage_increment = mem_usage - mem_usage_before
# Append results
results.append((matrix_size, elapsed_time, mem_usage_increment))
# Cleanup
del matrix_A, vector_B, vector_x
gc.collect()
# Save results to CSV
np.savetxt(
"numpy_results.csv",
results,
delimiter=",",
header="MatrixSize,Time,Memory",
comments="",
fmt=["%d", "%.6f", "%.6f"],
)