forked from realpython/python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_execution_time.py
More file actions
executable file
·53 lines (41 loc) · 1.04 KB
/
Copy path06_execution_time.py
File metadata and controls
executable file
·53 lines (41 loc) · 1.04 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
"""
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()
sample_list = []
for i in range(1, 1000000):
if i % 2 == 0:
sample_list.append(random.randint(1, 888898))
for i in range(10):
print("1111")
print('Finished in {} seconds.'.format(timer.duration()))