Benchmark multiple functions, with three different length int values.
()
| 50 | |
| 51 | |
| 52 | def benchmark() -> None: |
| 53 | """ |
| 54 | Benchmark multiple functions, with three different length int values. |
| 55 | """ |
| 56 | from collections.abc import Callable |
| 57 | from timeit import timeit |
| 58 | |
| 59 | def benchmark_a_function(func: Callable, value: int) -> None: |
| 60 | call = f"{func.__name__}({value})" |
| 61 | timing = timeit(f"__main__.{call}", setup="import __main__") |
| 62 | print(f"{call:56} = {func(value)} -- {timing:.4f} seconds") |
| 63 | |
| 64 | for value in (262144, 1125899906842624, 1267650600228229401496703205376): |
| 65 | for func in (sum_of_digits, sum_of_digits_recursion, sum_of_digits_compact): |
| 66 | benchmark_a_function(func, value) |
| 67 | print() |
| 68 | |
| 69 | |
| 70 | if __name__ == "__main__": |
no test coverage detected