-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.py
More file actions
48 lines (34 loc) · 1.21 KB
/
metrics.py
File metadata and controls
48 lines (34 loc) · 1.21 KB
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
from dataclasses import dataclass
import time
import tracemalloc
@dataclass
class Metrics:
elapsed_time: float
cpu_time: float
peak_memory_kb: int
nb_visited: int | None = None
def __str__(self) -> str:
line = f"Elapsed time: {self.elapsed_time:.6f} s" + " | " + f"CPU time: {self.cpu_time:.6f} s" + " | " + f"Peak memory: {self.peak_memory_kb} KB"
if self.nb_visited is not None:
line += " | " + f"Number of visits: {self.nb_visited}"
return line + "\n"
def get_metrics(solver, parameters):
"""
solver: function(parameters) -> (solution, stats)
stats: optional dict containing algorithm counters
"""
tracemalloc.start()
start_time = time.perf_counter()
start_cpu = time.process_time()
solution, capacity_left, stats = solver(parameters)
end_time = time.perf_counter()
end_cpu = time.process_time()
_, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()
metrics = Metrics(
elapsed_time=end_time - start_time,
cpu_time=end_cpu - start_cpu,
peak_memory_kb=peak // 1024,
nb_visited=stats.get("nb_visits") if stats is not None else None
)
return solution, capacity_left, metrics