-
Notifications
You must be signed in to change notification settings - Fork 9
/
ex_10_10.py
45 lines (37 loc) · 1.12 KB
/
ex_10_10.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
#!/usr/bin/python
#AUTHOR: alexxa
#DATE: 28.12.2013
#SOURCE: Think Python: How to Think Like a Computer Scientist by Allen B. Downey
# http://www.greenteapress.com/thinkpython/html/index.html
#PURPOSE: Chapter 10. Lists
# Exercise 10.
# Write a function that reads the file words.txt and builds
# a list with one element per word. Write two versions of this
# function, one using the append method and the other using
# the idiom t = t + [x]. Which one takes longer to run? Why?
# Hint: use the time module to measure elapsed time.
# Solution: http://thinkpython.com/code/wordlist.py .
import time
def build_list1():
fin = open('words.txt')
list1 = []
for line in fin:
word = line.strip()
list1.append(word)
return list1
def build_list2():
fin = open('words.txt')
list2 = []
for line in fin:
word = line.strip()
list2 = list2 + [word]
return list2
start_time = time.time()
build_list1()
function_time1 = time.time() - start_time
start_time = time.time()
build_list2()
function_time2 = time.time() - start_time
print(function_time1)
print(function_time2)
#END