aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms/timer.py
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms/timer.py')
-rw-r--r--algorithms/timer.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/algorithms/timer.py b/algorithms/timer.py
new file mode 100644
index 0000000..c567a3f
--- /dev/null
+++ b/algorithms/timer.py
@@ -0,0 +1,27 @@
+# Copyright (c) 2016 Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "Sorting algorithms" project.
+# For details, see https://github.com/egor-tensin/sorting-algorithms.
+# Distributed under the MIT License.
+
+import gc
+import time
+
+
+def get_timestamp():
+ return time.perf_counter()
+
+
+class Timer:
+ def __init__(self, dest, iterations=1):
+ self._dest = dest
+ self._iterations = iterations
+
+ def __enter__(self):
+ gc.disable()
+ self._start = get_timestamp()
+ return self
+
+ def __exit__(self, *args):
+ end = get_timestamp()
+ gc.enable()
+ self._dest.append((end - self._start) / self._iterations)