forked from UWPCE-PythonCert/IntroPython-2017
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.py
More file actions
45 lines (32 loc) · 1.09 KB
/
Copy pathtimer.py
File metadata and controls
45 lines (32 loc) · 1.09 KB
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
#!/usr/bin/env python
"""
Simple timing context manager
NOTE: this is only good for crude timing
-- use the timeit module for more accurate timing.
"""
import sys
import time
class Timer:
def __init__(self, outfile=sys.stdout, name=""):
self.outfile = outfile
self.name = " for " + name if name else ""
def __enter__(self):
self.start = time.clock()
def __exit__(self, exc_type, exc_val, exc_tb):
elapsed = time.clock() - self.start
self.outfile.write("Elapsed time{}: {:3g} seconds\n".format(self.name, elapsed))
if __name__ == "__main__":
# Examples of how to use it
# simplest
with Timer():
sum(range(100))
# giving a name to your block of code:
with Timer(name="very important code"):
sum(range(30000))
# sending it to a file:
with open("log.txt", 'a') as log:
with Timer(outfile=log, name="very important code"):
sum(range(30000))
# sending it to a file nested:
with open("log.txt", 'a') as log, Timer(outfile=log, name="very important code"):
sum(range(20000))