MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / benchmark

Function benchmark

maths/sum_of_digits.py:52–67  ·  view source on GitHub ↗

Benchmark multiple functions, with three different length int values.

()

Source from the content-addressed store, hash-verified

50
51
52def 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
70if __name__ == "__main__":

Callers 1

sum_of_digits.pyFile · 0.70

Calls 1

benchmark_a_functionFunction · 0.70

Tested by

no test coverage detected