8000 Add files via upload · realpython/python-scripts@502625d · GitHub
[go: up one dir, main page]

Skip to content

Commit 502625d

Browse files
authored
Add files via upload
1 parent cb448c2 commit 502625d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

06_execution_time02.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
ExecutionTime
3+
4+
This class is used for timing execution of code.
5+
6+
For example:
7+
8+
timer = ExecutionTime()
9+
print 'Hello world!'
10+
print 'Finished in {} seconds.'.format(timer.duration())
11+
12+
"""
13+
14+
15+
import time
16+
import random
17+
#
18+
#
19+
# class ExecutionTime:
20+
# def __init__(self):
21+
# self.start_time = time.time()
22+
#
23+
# def duration(self):
24+
# return time.time() - self.start_time
25+
#
26+
#
27+
# # ---- run code ---- #
28+
#
29+
#
30+
# timer = ExecutionTime()
31+
# sample_list = list()
32+
# my_list = [random.randint(1, 888898) for num in
33+
# range(1, 1000000) if num % 2 == 0]
34+
# print('Finished in {} seconds.'.format(timer.duration()))
35+
36+
37+
class ExecutionTime:
38+
def __init__(self):
39+
self.start_time = time.time()
40+
def duration(self):
41+
return time.time() - self.start_time
42+
43+
timer = ExecutionTime()
44+
my_list = []
45+
for num in range(1, 1000000):
46+
if num % 2 == 0:
47+
my_list.append(random.randint(1, 888898))
48+
print("Finished in {:.2f} seconds".format(timer.duration()))

0 commit comments

Comments
 (0)
0