Better handling of time

This commit is contained in:
David Doblas Jiménez 2021-12-19 17:25:51 +01:00
parent b5c4004959
commit 78abb7a5e8

View File

@ -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]
return str(num) == str(num)[::-1]