-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtimers.py
39 lines (31 loc) · 804 Bytes
/
timers.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from __future__ import print_function
# -*- coding: utf-8 -*-
import contextlib
import os
import sys
import time
class Timer(object):
def __init__(self):
self.start_time = time.time()
self.start_clock = self._clock()
def _clock(self):
times = os.times()
return times[0] + times[1]
def __str__(self):
return "[%.3fs CPU, %.3fs wall-clock]" % (
self._clock() - self.start_clock,
time.time() - self.start_time)
@contextlib.contextmanager
def timing(text, block=False):
timer = Timer()
if block:
print("%s..." % text)
else:
print("%s..." % text, end=' ')
sys.stdout.flush()
yield
if block:
print("%s: %s" % (text, timer))
else:
print(timer)
sys.stdout.flush()