-
Notifications
You must be signed in to change notification settings - Fork 1
/
executiontimer.py
65 lines (52 loc) · 1.96 KB
/
executiontimer.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
"""
Simple timer for profiling code execution time
@author: Roger Woodman
"""
import datetime
import time
class ExecutionTimer():
"""Simple timer class for profiling code execution time"""
def __init__(self, verboseOutput = False):
"""Starts execution timer
:param verboseOutput: If true, execution time output will be more descriptive
"""
self.verboseOutput = verboseOutput
self.timeStart = self.resetTimer()
self.timeEnd = -1
self.executionDuration = -1
def resetTimer(self):
"""Captures the current time as the start time"""
self.timeStart = datetime.datetime.now()
return self.timeStart
def stopTimer(self, printToScreen = True):
"""Stops timer
:param printToScreen: Print execution time to the screen
"""
# Calculate excecution duration
self.timeEnd = datetime.datetime.now()
self.executionDuration = self.timeEnd - self.timeStart
# Print the execution duration to the screen
if(printToScreen):
self.printExecutionTime()
return self.executionDuration
def getStartTime(self):
"""Get start time"""
return self.timeStart
def getEndTime(self):
"""Get end time"""
return self.timeEnd
def getExecutionDuration(self):
"""Get execution duration"""
return self.executionDuration
def printExecutionTime(self):
"""Prints execution time"""
if(self.verboseOutput):
print("Execution time: %s second(s) %s ms" % (self.executionDuration.seconds, self.executionDuration.microseconds / 1000))
else:
print("Execution time: %s" % self.executionDuration)
if __name__ == '__main__':
# Execution timer test
executionTimer = ExecutionTimer(verboseOutput = True)
time.sleep(42.42)
executionTimer.stopTimer()