-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWoods_Lawrence_Stop_Watch[1].py
More file actions
85 lines (48 loc) · 1.73 KB
/
Woods_Lawrence_Stop_Watch[1].py
File metadata and controls
85 lines (48 loc) · 1.73 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'''
Lawrence Woods
3/3/2018
Class StopWatch
(Stopwatch) Design a class named StopWatch. The class contains:
The private data fields startTime and endTime with get methods.
A constructor that initializes startTime with the current time.
A method named start() that resets the startTime to the current time.
A method named stop() that sets the endTime to the current time.
A method named getElapsedTime() that returns the elapsed time for the stop watch in milliseconds.
Draw the UML diagram for the class, and then implement the class.
Write a test program that measures the execution time of adding numbers from 1 to 1,000,000.
'''
import time
class Stopwatch:
# initialize the private fields of Stopwatch
def __init__(self):
self.startTime = time.clock()
self.endTime = None
# method to get the startTime
def getStartTime(self):
return self.startTime
# method to get the endTime
def getEndTime(self):
return self.endTime
# method to start the stopwatch
def start(self):
self.startTime = time.time()
# method to stop the stopwatch
def stop(self):
self.endTime = time.time()
# method to get the elapsedTime
def getElapsedTime(self):
elapsedTime = None;
if (self.endTime != None):
elapsedTime = (self.endTime - self.startTime) * 1000
return elapsedTime
# method to test the class
def main():
timer = Stopwatch()
timer.start()
sum = 0
for i in range(1, 1000001):
sum = sum + i
timer.stop()
print("Execution time :" + str(timer.getElapsedTime()) + " milliseconds");
# call the main method
main()