Bladeren bron

Modified Main.py to separate problem solving and benchmarking.

Bryan Allred 14 jaren geleden
bovenliggende
commit
72971f15b0
1 gewijzigde bestanden met toevoegingen van 202 en 42 verwijderingen
  1. 202 42
      src/Main.py

+ 202 - 42
src/Main.py

@ -16,21 +16,14 @@ import FibonacciRecursive
16 16
from StopWatch import StopWatch
17 17

18 18
def displayMenu():
19
    stopWatch = StopWatch()
20
    
21 19
    while True:
22 20
        print
23 21
        print "Fun with Fibonacci!!!"
24 22
        print
25
        print "Select your poison:"
23
        print "Operations:"
26 24
        print
27
        print "\t1. Binet's formula"
28
        print "\t2. Djikstra's method"
29
        print "\t3. Iteration"
30
        print "\t4. Lucas' numbers"
31
        print "\t5. Matrix"
32
        print "\t6. Memory"
33
        print "\t7. Recursive (intensive)"
25
        print "\t1. Find a specific number"
26
        print "\t2. Benchmark"
34 27
        print "\t-------------------------"
35 28
        print "\t0. Exit"
36 29
        print
@ -42,51 +35,218 @@ def displayMenu():
42 35
        if choice == "0":
43 36
            break
44 37
        
45
        # Let the user pick which number to find.
46
        n = -1
47
        while n < 0:
48
            n = int(raw_input("Number: "))
38
        if choice == "1":
39
            SingleTest()
40
        elif choice == "2":
41
            Benchmark()
42
        else:
43
            print "Incorrect selection."
44
        
45
        raw_input("Press ENTER to continue...")
46
    
47
    print
48
    print "Hope you had as much as I did!"
49
    print
50

51
def Benchmark():
52
    print
53
    print "Select your poison:"
54
    print
55
    print "\t1. Binet's formula"
56
    print "\t2. Djikstra's method"
57
    print "\t3. Iteration"
58
    print "\t4. Lucas' numbers"
59
    print "\t5. Matrix"
60
    print "\t6. Memory"
61
    print "\t7. Recursive (intensive)"
62
    print
63
    
64
    # Retrieve the user menu choice.
65
    choices = raw_input("Options (separated with spaces): ").split()
66
    
67
    # Let the user pick which number to find.
68
    n = -1
69
    while n < 0:
70
        n = int(raw_input("Number: "))
71
    
72
    # Start the timer.
73
    stopWatch = StopWatch()
49 74
        
50
        # Start the timer.
75
    if "1" in choices:
51 76
        stopWatch.Start()
52
        answer = "Not available."
77
        completed = 0
53 78
        
54 79
        try:
55
            # Perform the necessary method.
56
            if choice == "1":
57
                answer = FibonacciBinet.fib(n)
58
            elif choice == "2":
59
                answer = FibonacciDjikstra.fib(n)
60
            elif choice == "3":
61
                answer = FibonacciIteration.fib(n)
62
            elif choice == "4":
63
                answer = FibonacciLucas.fib(n)
64
            elif choice == "5":
65
                answer = FibonacciMatrix.fib(n)
66
            elif choice == "6":
67
                answer = FibonacciMemory.fib(n)
68
            elif choice == "7":
69
                answer = FibonacciRecursive.fib(n)
80
            print
81
            print "Binet's formula"
82
            
83
            while completed < n:
84
                FibonacciBinet.fib(completed)
85
                completed += 1
70 86
        except:
71 87
            pass
88
        finally:
89
            print "\tCompleted: {0}% in {1} ms".format((1.0 * completed / n) * 100, stopWatch.TotalMilliseconds())
90
            print
91
    
92
    if "2" in choices:
93
        stopWatch.Start()
94
        completed = 0
72 95
        
73
        # Stop the timer.
74
        stopWatch.Stop()
96
        try:
97
            print
98
            print "Djikstra's method"
99
            
100
            while completed < n:
101
                FibonacciDjikstra.fib(completed)
102
                completed += 1
103
        except:
104
            pass
105
        finally:
106
            print "\tCompleted: {0}% in {1} ms".format((1.0 * completed / n) * 100, stopWatch.TotalMilliseconds())
107
            print
108
    
109
    if "3" in choices:
110
        stopWatch.Start()
111
        completed = 0
75 112
        
76
        # Give the user some feedback and statistics.
77
        print
78
        print "Answer:\t\t{0}".format(answer)
79
        print "Time Elapsed:\t{0}".format(stopWatch.TimeElapsed())
80
        print
113
        try:
114
            print
115
            print "Iteration"
116
            
117
            while completed < n:
118
                FibonacciIteration.fib(n)
119
                completed += 1
120
        except:
121
            pass
122
        finally:
123
            print "\tCompleted: {0}% in {1} ms".format((1.0 * completed / n) * 100, stopWatch.TotalMilliseconds())
124
            print
125
    
126
    if "4" in choices:
127
        stopWatch.Start()
128
        completed = 0
81 129
        
82
        # Prepare the stop watch for the next iteration.
83
        stopWatch.Reset()
130
        try:
131
            print
132
            print "Lucas' numbers"
133
            
134
            while completed < n:
135
                FibonacciLucas.fib(n)
136
                completed += 1
137
        except:
138
            pass
139
        finally:
140
            print "\tCompleted: {0}% in {1} ms".format((1.0 * completed / n) * 100, stopWatch.TotalMilliseconds())
141
            print
142
    
143
    if "5" in choices:
144
        stopWatch.Start()
145
        completed = 0
84 146
        
85
        raw_input("Press ENTER to continue...")
147
        try:
148
            print
149
            print "Matrix"
150
            
151
            while completed < n:
152
                FibonacciMatrix.fib(n)
153
                completed += 1
154
        except:
155
            pass
156
        finally:
157
            print "\tCompleted: {0}% in {1} ms".format((1.0 * completed / n) * 100, stopWatch.TotalMilliseconds())
158
            print
159
    
160
    if "6" in choices:
161
        stopWatch.Start()
162
        completed = 0
163
        
164
        try:
165
            print
166
            print "Memory"
167
            
168
            while completed < n:
169
                FibonacciMemory.fib(n)
170
                completed += 1
171
        except:
172
            pass
173
        finally:
174
            print "\tCompleted: {0}% in {1} ms".format((1.0 * completed / n) * 100, stopWatch.TotalMilliseconds())
175
            print
86 176
    
177
    if "7" in choices:
178
        stopWatch.Start()
179
        completed = 0
180
        
181
        try:
182
            print
183
            print "Recursive (intensive)"
184
            
185
            while completed < n:
186
                FibonacciRecursive.fib(n)
187
                completed += 1
188
        except:
189
            pass
190
        finally:
191
            print "\tCompleted: {0}% in {1} ms".format((1.0 * completed / n) * 100, stopWatch.TotalMilliseconds())
192
            print
193

194
def SingleTest():
87 195
    print
88
    print "Hope you had as much as I did!"
196
    print "Select your poison:"
89 197
    print
198
    print "\t1. Binet's formula"
199
    print "\t2. Djikstra's method"
200
    print "\t3. Iteration"
201
    print "\t4. Lucas' numbers"
202
    print "\t5. Matrix"
203
    print "\t6. Memory"
204
    print "\t7. Recursive (intensive)"
205
    print
206
    
207
    # Retrieve the user menu choice.
208
    choice = raw_input("Option: ")
209
    
210
    # Let the user pick which number to find.
211
    n = -1
212
    while n < 0:
213
        n = int(raw_input("Number: "))
214
    
215
    # Start the timer.
216
    stopWatch = StopWatch()
217
    stopWatch.Start()
218
    answer = "Not available."
219
    
220
    try:
221
        # Perform the necessary method.
222
        if choice == "1":
223
            answer = FibonacciBinet.fib(n)
224
        elif choice == "2":
225
            answer = FibonacciDjikstra.fib(n)
226
        elif choice == "3":
227
            answer = FibonacciIteration.fib(n)
228
        elif choice == "4":
229
            answer = FibonacciLucas.fib(n)
230
        elif choice == "5":
231
            answer = FibonacciMatrix.fib(n)
232
        elif choice == "6":
233
            answer = FibonacciMemory.fib(n)
234
        elif choice == "7":
235
            answer = FibonacciRecursive.fib(n)
236
    except:
237
        pass
238
    
239
    # Stop the timer.
240
    stopWatch.Stop()
241
    
242
    # Give the user some feedback and statistics.
243
    print
244
    print "Answer:\t\t{0}".format(answer)
245
    print "Time Elapsed:\t{0} ms".format(stopWatch.TotalMilliseconds())
246
    print
247
    
248
    # Prepare the stop watch for the next iteration.
249
    stopWatch.Reset()
90 250

91 251
if __name__ == '__main__':
92 252
    displayMenu()