-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinear_time_1.py
More file actions
executable file
·37 lines (30 loc) · 1.06 KB
/
linear_time_1.py
File metadata and controls
executable file
·37 lines (30 loc) · 1.06 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
import time
import linear_search_1
import linear_search_2
import linear_search_3
def time_it(search, v, L):
'''Time how long it takes to run function search to find value v in list L.'''
t1 = time.time()
search(v, L)
t2 = time.time()
return (t2 - t1) * 1000.
def print_times(v, L):
'''Print the number of milliseconds it takes for linear_search(v, L)
to run for list.index, basic linear search, the for loop linear search,
and sentinel search.'''
# Get list.index's running time.
t1 = time.time()
L.index(v)
t2 = time.time()
index_time = (t2 - t1) * 1000.
# Get the other three running times.
basic_time = time_it(linear_search_1.linear_search, v, L)
for_time = time_it(linear_search_2.linear_search, v, L)
sentinel_time = time_it(linear_search_3.linear_search, v, L)
print "%d\t%.02f\t%.02f\t%.02f\t%.02f" % \
(v, basic_time, for_time, sentinel_time, index_time)
L = range(1000001)
linear_search_1.linear_search(10, L)
print_times(10, L)
print_times(500000, L)
print_times(1000000, L)