diff --git a/2024/Day00/main.py b/2024/Day00/main.py index b728c56..62a4d2b 100644 --- a/2024/Day00/main.py +++ b/2024/Day00/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day01/main.py b/2024/Day01/main.py index b728c56..62a4d2b 100644 --- a/2024/Day01/main.py +++ b/2024/Day01/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day02/main.py b/2024/Day02/main.py index b728c56..62a4d2b 100644 --- a/2024/Day02/main.py +++ b/2024/Day02/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day03/main.py b/2024/Day03/main.py index b728c56..62a4d2b 100644 --- a/2024/Day03/main.py +++ b/2024/Day03/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day04/main.py b/2024/Day04/main.py index b728c56..62a4d2b 100644 --- a/2024/Day04/main.py +++ b/2024/Day04/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day05/main.py b/2024/Day05/main.py index b728c56..62a4d2b 100644 --- a/2024/Day05/main.py +++ b/2024/Day05/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day06/main.py b/2024/Day06/main.py index b728c56..62a4d2b 100644 --- a/2024/Day06/main.py +++ b/2024/Day06/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day07/main.py b/2024/Day07/main.py index b728c56..62a4d2b 100644 --- a/2024/Day07/main.py +++ b/2024/Day07/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day08/main.py b/2024/Day08/main.py index b728c56..62a4d2b 100644 --- a/2024/Day08/main.py +++ b/2024/Day08/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day09/main.py b/2024/Day09/main.py index b728c56..62a4d2b 100644 --- a/2024/Day09/main.py +++ b/2024/Day09/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/Day10/main.py b/2024/Day10/main.py index b728c56..62a4d2b 100644 --- a/2024/Day10/main.py +++ b/2024/Day10/main.py @@ -6,7 +6,7 @@ from solutions import part1, part2 from utils.inputs import get_inputs -from utils.timings import profile_run +from utils.profiling import profile_run if __name__ == "__main__": input_path = f"{os.path.dirname(os.path.realpath(__file__))}/input.txt" diff --git a/2024/utils/timings.py b/2024/utils/profiling.py similarity index 53% rename from 2024/utils/timings.py rename to 2024/utils/profiling.py index bc0f3ff..54feff8 100644 --- a/2024/utils/timings.py +++ b/2024/utils/profiling.py @@ -1,6 +1,8 @@ +import os from time import perf_counter from typing import Callable, Tuple, TypeVar +import psutil from termcolor import colored T = TypeVar("T") @@ -18,13 +20,24 @@ def format_duration(milliseconds: float) -> str: def profile_run(name: str, func: Callable[[], T]) -> Tuple[T, float]: + process = psutil.Process(os.getpid()) # Get the current process + start_cpu = process.cpu_percent(interval=None) # Initial CPU usage + start_memory = process.memory_info().rss / (1024 * 1024) # Initial memory in MB + start = perf_counter() result = func() end = perf_counter() + + end_cpu = process.cpu_percent(interval=None) # Final CPU usage + end_memory = process.memory_info().rss / (1024 * 1024) # Final memory in MB + duration = (end - start) * 1000 + cpu_usage = end_cpu - start_cpu # CPU usage percentage + memory_change = end_memory - start_memory # Memory change in MB print( - f"{colored(name, 'magenta')}: {colored(result, 'green')} {colored(f'(executed in {format_duration(duration)})', 'light_grey', attrs=['dark'])}" + f"{colored(name, 'magenta')}: {colored(result, 'green')} " + f"{colored(f'(Time: {format_duration(duration)}, Memory: {memory_change:.2f}MB, CPU: {cpu_usage:.2f}%)', 'light_grey', attrs=['dark'])}" ) return result, duration