-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
52 lines (42 loc) · 1.19 KB
/
test.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
import time
path_to_file = 'corpus.txt'
with open(path_to_file, 'r') as f:
text = f.read()
# Loop through lines and words in lines and keep track of longest
start = int(round(time.time() * 1000))
longest = ''
lines = text.split('\n')
for line in lines:
for word in line.split(' '):
if len(word) > len(longest):
longest = word
end = int(round(time.time() * 1000)) - start
print(longest)
print('Loop and keep track of longest:\n{} ms\n\n'.format(end))
# Loop through lines and words to create list. Sort list.
start = int(round(time.time() * 1000))
lines = text.split('\n')
words = set()
for line in lines:
for word in line.split(' '):
words.add(word)
end = int(round(time.time() * 1000)) - start
print(sorted(
words,
key=lambda k: len(k),
reverse=True
)[0])
print('Loop to build list and sort:\n{} ms\n\n'.format(end))
# Use comprehension to build list and sort it
start = int(round(time.time() * 1000))
print(sorted(
set([
x
for t in text.split('\n')
for x in t.split(' ')
]),
key=lambda k: len(k),
reverse=True
)[0])
end = int(round(time.time() * 1000)) - start
print('Use comprehensions:\n{} ms\n\n'.format(end))