From 0c5f41b2b3d738730dc24906514e506064031dc8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 20 Oct 2016 09:13:33 +0530 Subject: [PATCH] timeit() context manager to measure time for executing a block of code --- kitty/utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/kitty/utils.py b/kitty/utils.py index 7bbd55362..0c0ef3d59 100644 --- a/kitty/utils.py +++ b/kitty/utils.py @@ -11,7 +11,9 @@ import fcntl import signal import ctypes import unicodedata +from contextlib import contextmanager from functools import lru_cache +from time import monotonic from PyQt5.QtGui import QFontMetrics @@ -97,3 +99,12 @@ base_size = sys.getsizeof('') def is_simple_string(x): ' We use the fact that python stores unicode strings with a 1-byte representation when possible ' return sys.getsizeof(x) == base_size + len(x) + + +@contextmanager +def timeit(name, do_timing=False): + if do_timing: + st = monotonic() + yield + if do_timing: + print('Time for {}: {}'.format(name, monotonic() - st))