diff --git a/solutions/0056.PowerfulDigitSum/euler_56.py b/solutions/0056.PowerfulDigitSum/euler_56.py index ef30316..cacb080 100644 --- a/solutions/0056.PowerfulDigitSum/euler_56.py +++ b/solutions/0056.PowerfulDigitSum/euler_56.py @@ -7,7 +7,6 @@ Considering natural numbers of the form, a^b , where a,b < 100 , what is the max """ import time -from decimal import MAX_EMAX from functools import wraps from typing import Any, Callable, TypeVar diff --git a/solutions/0057.SquareRoot/euler_57.py b/solutions/0057.SquareRoot/euler_57.py new file mode 100644 index 0000000..52ff584 --- /dev/null +++ b/solutions/0057.SquareRoot/euler_57.py @@ -0,0 +1,40 @@ +''' + +''' +import time +from functools import wraps +from typing import Any, Callable, TypeVar + +F = TypeVar("F", bound=Callable[..., Any]) + + +def benchmark(repeat: int = 1) -> Callable[[F], F]: + if repeat < 1: + raise ValueError("repeat must >= 1") + + def decorator(func: F) -> F: + @wraps(func) + def wrapper(*args: Any, **kwargs: Any) -> Any: + total = 0.0 + result = None + + for _ in range(repeat): + start = time.perf_counter() + result = func(*args, **kwargs) + end = time.perf_counter() + total += end - start + + wrapper.avg_time = total / repeat # type: ignore[attr-defined] + wrapper.total_time = total # type: ignore[attr-defined] + + print( + f"[Benchmark] {func.__name__} | repeated {repeat} times | " + f"average: {wrapper.avg_time:.6f}s | total: {wrapper.total_time:.6f}s" # type: ignore[attr-defined] + ) + return result + + wrapper.avg_time = 0.0 # type: ignore[attr-defined] + wrapper.total_time = 0.0 # type: ignore[attr-defined] + return wrapper # type: ignore[return-value] + + return decorator \ No newline at end of file