From dbda53d19ac3629699ee562cc743b3d01f7f7d72 Mon Sep 17 00:00:00 2001 From: Gouvernathor <44340603+Gouvernathor@users.noreply.github.com> Date: Sat, 11 Nov 2023 21:02:36 +0100 Subject: [PATCH] Update the timeit function to use the timeit module add the autorange function as a bonus (I like it better) --- game/scripts/utility/common_functions.rpy | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/game/scripts/utility/common_functions.rpy b/game/scripts/utility/common_functions.rpy index 03a883ab..6936d1e5 100644 --- a/game/scripts/utility/common_functions.rpy +++ b/game/scripts/utility/common_functions.rpy @@ -11,6 +11,7 @@ init python early: import re import string import functools + import timeit as timeit_module from bisect import bisect from operator import itemgetter from operator import add as _add @@ -138,11 +139,12 @@ init python early: return False def timeit(func, loops=10000, args=(), kwargs={}): - start = time.time() - for i in range(loops): - func(*args, **kwargs) - end = time.time() - print(f"The task has taken {end-start} seconds to finish") + rv = timeit_module.timeit("func(*args, **kwargs)", number=loops, globals=dict(func=func, args=args, kwargs=kwargs)) + print(f"The task has taken {rv} seconds to finish") + + def autorange(func, args=(), kwargs={}): + loops, time = timeit_module.Timer("func(*args, **kwargs)", globals=dict(func=func, args=args, kwargs=kwargs)).autorange() + print(f"The task has taken {time/loops} seconds to finish ({loops} iterations in {time} seconds)") def list_swap_values(l, val1, val2): """Mutates the original list.""" @@ -222,4 +224,4 @@ init python early: return len(self._callable()) def execute_callbacks(callbacks): - for callback in callbacks: callback() \ No newline at end of file + for callback in callbacks: callback()