Last active
November 1, 2023 20:03
-
-
Save n0nuser/e29ebf0b908d2214d9ace337e9d1ee76 to your computer and use it in GitHub Desktop.
Python Function Performance Profiler - Should work as is!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from profiler import profile_functions | |
def romanToIntPablo2022(s: str) -> int: | |
roman = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} | |
roman_sum = 0 | |
for index, letter in enumerate(s): | |
try: | |
if roman[letter] < roman[s[index + 1]]: | |
roman_sum -= roman[letter] | |
else: | |
roman_sum += roman[letter] | |
except IndexError: | |
roman_sum += roman[letter] | |
return roman_sum | |
def romanToIntPablo2023(s: str) -> int: | |
roman = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000} | |
result = 0 | |
prev_value = 0 | |
for i in range(len(s) - 1, -1, -1): | |
curr_value = roman[s[i]] | |
if curr_value < prev_value: | |
result -= curr_value | |
else: | |
result += curr_value | |
prev_value = curr_value | |
return result | |
test_elements = [ | |
"III", | |
"LVIII", | |
"MCMXCIV", | |
"I", | |
"II", | |
"IV", | |
"VI", | |
"IX", | |
"XI", | |
"XIV", | |
"XVI", | |
"XIX", | |
"XXI", | |
"XXIV", | |
"XXVI", | |
"XXIX", | |
"XXXI", | |
"XXXIV", | |
"XXXVI", | |
"XXXIX", | |
"XL", | |
"XLIV", | |
"XLV", | |
"XLVII", | |
"L", | |
"LIV", | |
"LVI", | |
"LIX", | |
"LXI", | |
"LXIV", | |
] | |
functions = { | |
"Pablo - 2022": romanToIntPablo2022, | |
"Pablo - 2023": romanToIntPablo2023, | |
} | |
profile_functions(functions, test_elements, 500000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import platform | |
from time import perf_counter | |
from typing import Any, Callable, Dict, List | |
if platform.system() == "Linux": | |
try: | |
from hwcounter import count, count_end | |
except ImportError: | |
print( | |
"hwcounter package not found. Please install it to use the profiler on Linux." | |
) | |
exit(1) | |
# Class Decorator | |
class profiler: | |
def __enter__(self): | |
self.start_time = perf_counter() | |
if platform.system() == "Linux": | |
self.start_cycles = count() | |
return self | |
def __exit__(self, type, value, traceback): | |
self.total_time = perf_counter() - self.start_time | |
if platform.system() == "Linux": | |
self.total_cycles = count_end() - self.start_cycles | |
def profile_functions(functions: Dict[str, Callable], test_elements: List[Any], n_times:int = 500000) | |
print(f"Running {n_times} times for performance comparison...") | |
for name, func in functions.items(): | |
print(f"\n{name}:") | |
if platform.system() == "Linux": | |
mean_cycles = 0 | |
mean_time = 0 | |
for _ in range(n_times): | |
with profiler() as p: | |
for element in test_elements: | |
func(element) | |
mean_time += p.total_time | |
if platform.system() == "Linux": | |
mean_cycles += p.total_cycles | |
mean_time /= n_times | |
print(f"\tMean time: {mean_time}") | |
if platform.system() == "Linux": | |
mean_cycles /= n_times | |
print(f"\tMean cycles: {mean_cycles}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment