Created
September 6, 2019 07:50
-
-
Save veeenu/fd08c0d19c2b285ac99c2a228ccc4959 to your computer and use it in GitHub Desktop.
Python simple performance timer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import perf_counter | |
class PerfTimer: | |
def __init__(self): | |
self.entries = [] | |
self.last = None | |
def section(self, name): | |
clk = self.stop() | |
self.last = (name, clk) | |
def stop(self): | |
clk = perf_counter() | |
if self.last is not None: | |
ent = (self.last[0], clk - self.last[1]) | |
print(f'{ent[0]:20s} {ent[1]:.2f}s') | |
self.entries.append(ent) | |
return clk |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment