Last active
March 27, 2025 09:23
-
-
Save Tapanhaz/ac143fc3012a4f07faf1963ee40e4254 to your computer and use it in GitHub Desktop.
Compare if-else, match-case, dict lookup
This file contains hidden or 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 timeit | |
| import random | |
| def if_elif_else_lookup(n): | |
| if n == 0: | |
| return "zero" | |
| elif n == 1: | |
| return "one" | |
| elif n == 2: | |
| return "two" | |
| elif n == 3: | |
| return "three" | |
| elif n == 4: | |
| return "four" | |
| elif n == 5: | |
| return "five" | |
| elif n == 6: | |
| return "six" | |
| elif n == 7: | |
| return "seven" | |
| elif n == 8: | |
| return "eight" | |
| elif n == 9: | |
| return "nine" | |
| else: | |
| return "unknown" | |
| def match_case_lookup(n): | |
| match n: | |
| case 0: return "zero" | |
| case 1: return "one" | |
| case 2: return "two" | |
| case 3: return "three" | |
| case 4: return "four" | |
| case 5: return "five" | |
| case 6: return "six" | |
| case 7: return "seven" | |
| case 8: return "eight" | |
| case 9: return "nine" | |
| case _: return "unknown" | |
| lookup_dict = { | |
| 0: "zero", | |
| 1: "one", | |
| 2: "two", | |
| 3: "three", | |
| 4: "four", | |
| 5: "five", | |
| 6: "six", | |
| 7: "seven", | |
| 8: "eight", | |
| 9: "nine" | |
| } | |
| def dict_lookup_safe(n): | |
| return lookup_dict.get(n, "unknown") | |
| def dict_lookup_direct(n): | |
| return lookup_dict[n] | |
| def benchmark(total_keys: int, shuffle: bool= False, num_loops:int = 1_000_000)-> None: | |
| if total_keys > 10 or total_keys < 0: | |
| raise ValueError("Total keys must be between 1 and 10 (inclusive)") | |
| keys = list(range(total_keys)) | |
| print(f" Benchmarking With {total_keys} Keys ".center(60, "=")) | |
| if shuffle: | |
| random.shuffle(keys) | |
| print(f" Shuffle On ".center(60, "=")) | |
| else: | |
| print(f" Shuffle Off ".center(60, "=")) | |
| time_if_else = timeit.timeit(lambda: [if_elif_else_lookup(n) for n in keys], number=num_loops) | |
| time_match_case = timeit.timeit(lambda: [match_case_lookup(n) for n in keys], number=num_loops) | |
| time_dict_lookup = timeit.timeit(lambda: [dict_lookup_safe(n) for n in keys], number=num_loops) | |
| time_dict_lookup_direct = timeit.timeit(lambda: [dict_lookup_direct(n) for n in keys], number=num_loops) | |
| print(f"=> If-Elif-Else Lookup : {time_if_else:.6f} seconds") | |
| print(f"=> Match-Case Lookup : {time_match_case:.6f} seconds") | |
| print(f"=> Dict Lookup (Safe) : {time_dict_lookup:.6f} seconds") | |
| print(f"=> Dict Lookup (Direct): {time_dict_lookup_direct:.6f} seconds") | |
| print("*"* 60, "\n") | |
| if __name__ == "__main__": | |
| benchmark(10) | |
| benchmark(6) | |
| benchmark(3) | |
| benchmark(10, shuffle=True) | |
| benchmark(6, shuffle=True) | |
| benchmark(3, shuffle=True) | |
This file contains hidden or 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
| ================ Benchmarking With 10 Keys ================= | |
| ======================= Shuffle Off ======================== | |
| => If-Elif-Else Lookup : 0.794201 seconds | |
| => Match-Case Lookup : 0.810568 seconds | |
| => Dict Lookup (Safe) : 0.650343 seconds | |
| => Dict Lookup (Direct): 0.588400 seconds | |
| ************************************************************ | |
| ================= Benchmarking With 6 Keys ================= | |
| ======================= Shuffle Off ======================== | |
| => If-Elif-Else Lookup : 0.407205 seconds | |
| => Match-Case Lookup : 0.430830 seconds | |
| => Dict Lookup (Safe) : 0.421545 seconds | |
| => Dict Lookup (Direct): 0.374064 seconds | |
| ************************************************************ | |
| ================= Benchmarking With 3 Keys ================= | |
| ======================= Shuffle Off ======================== | |
| => If-Elif-Else Lookup : 0.213690 seconds | |
| => Match-Case Lookup : 0.224360 seconds | |
| => Dict Lookup (Safe) : 0.244685 seconds | |
| => Dict Lookup (Direct): 0.221933 seconds | |
| ************************************************************ | |
| ================ Benchmarking With 10 Keys ================= | |
| ======================== Shuffle On ======================== | |
| => If-Elif-Else Lookup : 0.779051 seconds | |
| => Match-Case Lookup : 0.792586 seconds | |
| => Dict Lookup (Safe) : 0.665903 seconds | |
| => Dict Lookup (Direct): 0.589343 seconds | |
| ************************************************************ | |
| ================= Benchmarking With 6 Keys ================= | |
| ======================== Shuffle On ======================== | |
| => If-Elif-Else Lookup : 0.407176 seconds | |
| => Match-Case Lookup : 0.433760 seconds | |
| => Dict Lookup (Safe) : 0.420259 seconds | |
| => Dict Lookup (Direct): 0.376265 seconds | |
| ************************************************************ | |
| ================= Benchmarking With 3 Keys ================= | |
| ======================== Shuffle On ======================== | |
| => If-Elif-Else Lookup : 0.218346 seconds | |
| => Match-Case Lookup : 0.228449 seconds | |
| => Dict Lookup (Safe) : 0.246668 seconds | |
| => Dict Lookup (Direct): 0.224166 seconds | |
| ************************************************************ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment