Is there a significant calculation time difference between recursive-style Fibonacci versus loop-style Fibonacci? I keep Fibonacci running again at 40 places and then using a loop straight back later. It seems that computation time difference is only academic .
written c
recursive resolution:
int main (int argc, const char * argv []) { Int n, i = 0, c; Printf ("Please enter an integer: \ n"); Scanf ("% d", & amp; n); {Printf ("% lu", fibonacci (i)) for (c = 1; c & lt; = n; c ++); I ++; } Return 0; } Long Fibonacci (long n) {if (n == 0) 0; And if (n == 1) return 1; And return (Fibonacci (N-1) + Fibonacci (N-2)); }; For the loop solution,
int main (int argc, const char * argv []) {int n, first = 0, second = 1, Next, c; Printf ("Please enter an integer: \ n"); Scanf ("% d", & amp; n); For (c = 0; c & lt; n; c ++) {if (c & lt; = 1) next = c; Else {next = first + second; First = second; Second = next; } Printf ("% d", next); } Return 0; }; The traditional recursion method tail is much slower than recurrent and iterative editions.
For the recurrence version enter the loop in the example code below with an open Loop is used. For the 32-bit signed integer, the 64 bit is the limit for the signed integer (47), the boundary is the fib (93).
The time was made with Intel 2600K 3.4ghz, XP X64, 64bit mode. XP or XP X64 High Performance Counter Frequency CPU Clock, is similar to 3.4 hours, but operating system overhead (such as interrupts), affects the duration if the period is small.
Time for Fib (40): microseconds of microsoft 0.2 fibi () # 0.2 for microsounds
Time for 94 loops, n = 0 to 93: / P>
Filt () # microsoft's 7 fibi () # microsoft 5
example code:
typedef unsigned long Up to UI 64; UI64 fibr (UI64 N) {if (n & lt; 2) returns n; Return fiber (N-1) + fiber (N-2); } // Call with Fib (N, 0, 1) UI64 Fib (UI 64 N, UI64 REE, UI64 Next) {if (N == 0) Return Ridge; Return Fibet (N-1, Next, Raise + Next); } UI64 FBI (UI64 N) {UI64 f0, f1, i; If (n & lt; 2) return n; N - = 2; F1 = F 0 = 1; I = 0; Switch (N% 8) {do {f1 + = f0; Case 7: f0 + = F1; Case 6: F1 + = F 0; Case 5: F 0 + = F1; Case 4: F1 + = F 0; Case 3: f0 + = F1; Case 2: F1 + = F 0; Case 1: f0 + = f1; Case 0: Continue; } While (n> = (i + = 8)); } Return f0; }
Alternative version of Faby (), without & n; Does the F and F1 represent the change within the loop ending with the final amount in the Future, then the initial position representing F and F1 depends, if n is too odd or not. If n also, f0 = fib (0) = 0, f1 = fib (-1) = 1, if n is odd, f1 = fib (0) = 0, f 0 = fib (-1) = 1 (If you are curious, Fib (-1) = 1, Fib (-2) = -1, Fib (-3) = 2, Fib (-4) = -3, Fib (-5) = 5, Fib (-6) = -8, ...).
To explain the reasoning here, even for any case, fib (-1) = f1 = 1, fiber (0) = f0 = 0, then fib (1) = (F1 + f0), fib (2) = (f0 + f1), fib (3) = (f1 + f0), fib (4) = (f0 + f1), ....
UI64 file (UI64N) {UI64 f0, f1, i; F0 = N & amp; 1; // if n also, f0 = 0, f1 = 1 f1 = 1 - f0; // else f1 = 0, f0 = 1 i = 0; Switch (N% 8) {do {f1 + = f0; Case 7: f0 + = F1; Case 6: F1 + = F 0; Case 5: F 0 + = F1; Case 4: F1 + = F 0; Case 3: f0 + = F1; Case 2: F1 + = F 0; Case 1: f0 + = f1; Case 0: Continue; } While (n> = (i + = 8)); } Return f0; }
No comments:
Post a Comment