Skip to content

Instantly share code, notes, and snippets.

@veeenu
Created September 6, 2019 07:50
Show Gist options
  • Save veeenu/fd08c0d19c2b285ac99c2a228ccc4959 to your computer and use it in GitHub Desktop.
Save veeenu/fd08c0d19c2b285ac99c2a228ccc4959 to your computer and use it in GitHub Desktop.
Python simple performance timer
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