forked from realpython/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_execution_time02.py
More file actions
48 lines (39 loc) · 1.02 KB
/
06_execution_time02.py
File metadata and controls
48 lines (39 loc) · 1.02 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
"""
ExecutionTime
This class is used for timing execution of code.
For example:
timer = ExecutionTime()
print 'Hello world!'
print 'Finished in {} seconds.'.format(timer.duration())
"""
import time
import random
#
#
# class ExecutionTime:
# def __init__(self):
# self.start_time = time.time()
#
# def duration(self):
# return time.time() - self.start_time
#
#
# # ---- run code ---- #
#
#
# timer = ExecutionTime()
# sample_list = list()
# my_list = [random.randint(1, 888898) for num in
# range(1, 1000000) if num % 2 == 0]
# print('Finished in {} seconds.'.format(timer.duration()))
class ExecutionTime:
def __init__(self):
self.start_time = time.time()
def duration(self):
return time.time() - self.start_time
timer = ExecutionTime()
my_list = []
for num in range(1, 1000000):
if num % 2 == 0:
my_list.append(random.randint(1, 888898))
print("Finished in {:.2f} seconds".format(timer.duration()))