-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
64 lines (50 loc) · 1.69 KB
/
search.py
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
import argparse
import random
import time
import utils
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('-n',
dest='n',
type=int,
required=True,
help='Number of elements in the list')
parser.add_argument('--num_searches',
type=int,
required=True,
help='Number of queires')
return parser.parse_args()
def main():
args = get_args()
L = random.sample(range(1, args.n*1000), args.n)
linear_search_total_time = 0
for i in range(args.num_searches):
k = random.randint(1, args.n*1000)
tic = time.perf_counter()
i = utils. linear_search(L, k)
toc = time.perf_counter()
linear_search_total_time += toc - tic
linear_search_mean_time = linear_search_total_time / args.num_searches
print('Linear search: '
+ '{:.5f}'.format(linear_search_mean_time)
+ ' seconds')
tic = time.perf_counter()
I_idx = utils.index_list(L)
toc = time.perf_counter()
bsearch_idx_time = toc - tic
print('Indexing: '
+ '{:.5f}'.format(bsearch_idx_time)
+ ' seconds')
bsearch_total_time = 0
for i in range(args.num_searches):
k = random.randint(1, args.n*1000)
tic = time.perf_counter()
i = utils.binary_search(I_idx, k)
toc = time.perf_counter()
bsearch_total_time += toc - tic
bsearch_mean_time = bsearch_total_time / args.num_searches
print('Binary search: '
+ '{:.5f}'.format(bsearch_mean_time)
+ ' seconds')
if __name__ == '__main__':
main()