-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.py
71 lines (61 loc) · 2.06 KB
/
profile.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
"""
Author: Leigh Stauffer
Project: 3
File Name: profile.py
Resources for profiling sort and fibonacci functions.
"""
import time
from tools import Counter, getRandomList
from fibs import fib1, fib2
from sorts import selectionSort, quickSort
def profileSort(sortFunction, upper = 10):
"""Displays counts of comparisons and swaps
and running times for the given sort algorithm
on several data sizes. The data size starts at 1
and doubles on each iteration. An initial run
shows results for a sorted list of 256 numbers.
Arguments: the sort function and the number of data sets."""
comps = Counter()
swaps = Counter()
lyst = list(range(1, 257))
t1 = time.time()
sortFunction(lyst, comps, swaps)
t2 = time.time() - t1
print("Results for a sorted list of 256 numbers:")
print("Comparisons: ", comps)
print("Swaps: ", swaps)
print("Time in seconds:", t2)
size = 1
print("\n Size Comparisons Swaps Running Time (sec)")
for i in range(1, upper + 1):
comps.reset()
swaps.reset()
lyst = getRandomList(size)
t1 = time.time()
sortFunction(lyst, comps, swaps)
t2 = time.time() - t1
print("%5d%12s%12s%15.6f" % (size, comps, swaps, t2))
size *= 2
def profileFib(fibFunction, upper = 20):
"""Displays counts of calls and running times
of the given fib function on several values of n.
Arguments: the fib function and the upper bound
on n. n increases by 1 on each iteration."""
n = 1
counter = Counter()
print(" n fib(n) # Call Running Time (sec)")
for i in range(upper):
counter.reset()
t1 = time.time()
result = fibFunction(n, counter)
t2 = time.time() - t1
print("%3d%8d%12s%14.6f" % (n, result, counter, t2))
n += 1
def main():
"""Starting point for the profiler app."""
profileSort(selectionSort)
## profileSort(quickSort)
## profileFib(fib1)
## profileFib(fib2)
if __name__ == "__main__":
main()