brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.4 KiB · 6548086 Raw
52 lines · python
1# DExTer : Debugging Experience Tester2# ~~~~~~   ~         ~~         ~   ~~3#4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.5# See https://llvm.org/LICENSE.txt for license information.6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception7"""RAII-style timer class to be used with a 'with' statement to get wall clock8time for the contained code.9"""10 11import sys12import time13 14 15def _indent(indent):16    return "| " * indent17 18 19class Timer(object):20    fn = sys.stdout.write21    display = False22    indent = 023 24    def __init__(self, name=None):25        self.name = name26        self.start = self.now27 28    def __enter__(self):29        Timer.indent += 130        if Timer.display and self.name:31            indent = _indent(Timer.indent - 1) + " _"32            Timer.fn("{}\n".format(_indent(Timer.indent - 1)))33            Timer.fn("{} start {}\n".format(indent, self.name))34        return self35 36    def __exit__(self, *args):37        if Timer.display and self.name:38            indent = _indent(Timer.indent - 1) + "|_"39            Timer.fn(40                "{} {} time taken: {:0.1f}s\n".format(indent, self.name, self.elapsed)41            )42            Timer.fn("{}\n".format(_indent(Timer.indent - 1)))43        Timer.indent -= 144 45    @property46    def elapsed(self):47        return self.now - self.start48 49    @property50    def now(self):51        return time.time()52