aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms/timer.py
blob: bd5d044aa8227640fbcadc6c3b2ce4112c48371c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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)