Skip to content

Instantly share code, notes, and snippets.

@n0nuser
Last active November 1, 2023 20:03
Show Gist options
  • Select an option

  • Save n0nuser/e29ebf0b908d2214d9ace337e9d1ee76 to your computer and use it in GitHub Desktop.

Select an option

Save n0nuser/e29ebf0b908d2214d9ace337e9d1ee76 to your computer and use it in GitHub Desktop.

Revisions

  1. n0nuser revised this gist Nov 1, 2023. 2 changed files with 106 additions and 85 deletions.
    69 changes: 69 additions & 0 deletions functions.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    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)
    122 changes: 37 additions & 85 deletions profiler.py
    Original file line number Diff line number Diff line change
    @@ -1,95 +1,47 @@
    from hwcounter import count, count_end
    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()
    self.start_cycles = count()
    if platform.system() == "Linux":
    self.start_cycles = count()
    return self

    def __exit__(self, type, value, traceback):
    self.total_time = perf_counter() - self.start_time
    self.total_cycles = count_end() - self.start_cycles


    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,
    }
    n_times = 500000
    print(f"Running {n_times} times for performance comparison...")
    for name, func in functions.items():
    print(f"\n{name}:")
    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
    mean_cycles += p.total_cycles
    mean_time /= n_times
    mean_cycles /= n_times
    print(f"\tMean time: {mean_time}")
    print(f"\tMean cycles: {mean_cycles}")
    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}")
  2. n0nuser created this gist Nov 1, 2023.
    95 changes: 95 additions & 0 deletions profiler.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    from hwcounter import count, count_end
    from time import perf_counter


    class profiler:
    def __enter__(self):
    self.start_time = perf_counter()
    self.start_cycles = count()
    return self

    def __exit__(self, type, value, traceback):
    self.total_time = perf_counter() - self.start_time
    self.total_cycles = count_end() - self.start_cycles


    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,
    }
    n_times = 500000
    print(f"Running {n_times} times for performance comparison...")
    for name, func in functions.items():
    print(f"\n{name}:")
    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
    mean_cycles += p.total_cycles
    mean_time /= n_times
    mean_cycles /= n_times
    print(f"\tMean time: {mean_time}")
    print(f"\tMean cycles: {mean_cycles}")