From 78abb7a5e8f914b9389d96418bf8f0c5ef5adc0b Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sun, 19 Dec 2021 17:25:51 +0100 Subject: [PATCH] Better handling of time --- src/Python/utils.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Python/utils.py b/src/Python/utils.py index 6d70861..a06df8a 100644 --- a/src/Python/utils.py +++ b/src/Python/utils.py @@ -5,19 +5,31 @@ from functools import wraps def timeit(name): def profile(original): import time + @wraps(original) def wrapper(*args, **kwargs): t0 = time.perf_counter() result = original(*args, **kwargs) t1 = time.perf_counter() - print(f"Time to evaluate problem {int(name[7:]):003d}: {(t1 - t0)*1000:.3f} ms\n") + if (t1 - t0) > 1: + print( + f"Time to evaluate problem {int(name[7:]):003d}: " + f"{(t1 - t0):.3f} s\n" + ) + else: + print( + f"Time to evaluate problem {int(name[7:]):003d}: " + f"{(t1 - t0)*1000:.3f} ms\n" + ) return result + return wrapper + return profile def is_prime(n): - if n <2: + if n < 2: return False for i in range(2, int(math.sqrt(n)) + 1): if n % i == 0: @@ -45,4 +57,4 @@ def list_primes(n): def is_palindrome(num): - return str(num) == str(num)[::-1] \ No newline at end of file + return str(num) == str(num)[::-1]