-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
47 lines (35 loc) · 1.09 KB
/
benchmark.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
40
41
42
43
44
45
46
47
import datetime as dt
# from typing import *
class AlreadyStartedError(ValueError):
pass
class NotStartedError(ValueError):
pass
class StillRunningError(ValueError):
pass
class BenchMark:
def __init__(self, name):
# type: (str) -> ()
self.name = name
self.delta = dt.timedelta()
self.t_started = None
def start(self):
# type: () -> ()
if self.t_started is not None or self.delta.total_seconds() != 0:
raise AlreadyStartedError(self.name)
else:
self.t_started = dt.datetime.now()
def stop(self):
# type: () -> ()
if self.t_started is None:
raise NotStartedError(self.name)
else:
self.delta = dt.datetime.now() - self.t_started
self.t_started = None
def time(self):
return self.delta.total_seconds()
def result(self):
# type: () -> str
if self.t_started is not None:
raise StillRunningError(self.name)
else:
return "%s took %f seconds" % (self.name, self.delta.total_seconds())