-
Notifications
You must be signed in to change notification settings - Fork 145
/
Copy pathtuneup.py
99 lines (76 loc) · 2.69 KB
/
tuneup.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Tuneup assignment
Use the timeit and cProfile libraries to find bad code.
"""
__author__ = "???"
import cProfile
import pstats
import functools
def profile(func):
"""A cProfile decorator function that can be used to
measure performance.
"""
# Be sure to review the lesson material on decorators.
# You need to understand how they are constructed and used.
raise NotImplementedError("Complete this decorator function")
def read_movies(src):
"""Returns a list of movie titles."""
print(f'Reading file: {src}')
with open(src, 'r') as f:
return f.read().splitlines()
def is_duplicate(title, movies):
"""Returns True if title is within movies list."""
for movie in movies:
if movie.lower() == title.lower():
return True
return False
def find_duplicate_movies(src):
"""Returns a list of duplicate movies from a src list."""
# Not optimized
movies = read_movies(src)
duplicates = []
while movies:
movie = movies.pop()
if is_duplicate(movie, movies):
duplicates.append(movie)
return duplicates
#
# Students: write a better version of find_duplicate_movies
#
def optimized_find_duplicate_movies(src):
# Your code here
return
def timeit_helper(func_name, func_param):
"""Part A: Obtain some profiling measurements using timeit"""
assert isinstance(func_name, str)
# stmt = ???
# setup = ???
# t = ???
# runs_per_repeat = 3
# num_repeats = 5
# result = t.repeat(repeat=num_repeats, number=runs_per_repeat)
# time_cost = ???
# print(f"func={func_name} num_repeats={num_repeats} runs_per_repeat={runs_per_repeat} time_cost={time_cost:.3f} sec")
# return t
def main():
"""Computes a list of duplicate movie entries."""
# Students should not run two profiling functions at the same time,
# e.g. they should not be running 'timeit' on a function that is
# already decorated with @profile
filename = 'movies.txt'
print("--- Before optimization ---")
result = find_duplicate_movies(filename)
print(f'Found {len(result)} duplicate movies:')
print('\n'.join(result))
print("\n--- Timeit results, before optimization ---")
timeit_helper('find_duplicate_movies', filename)
print("\n--- Timeit results, after optimization ---")
timeit_helper('optimized_find_duplicate_movies', filename)
print("\n--- cProfile results, before optimization ---")
profile(find_duplicate_movies)(filename)
print("\n--- cProfile results, after optimization ---")
profile(optimized_find_duplicate_movies)(filename)
if __name__ == '__main__':
main()
print("Completed.")