222 Methods Chapter 6 see, the number of (Windows 2003 server web)

222 Methods Chapter 6 see, the number of calls to Fibonacci increases quickly 1,664,080 additional calls between the Fibonacci values of 30 and 31, and 2,692,538 additional calls between the Fibonacci values of 31 and 32. This difference in number of calls made between the Fibonacci values of 31 and 32 is more than 1.5 times the difference for Fibonacci values of 30 and 31. Problems of this nature humble even the world s most powerful computers! In the field called complexity theory, computer scientists determine how hard algorithms work to do their jobs. Complexity issues are discussed in detail in the upper-level computer science curriculum course generally called Algorithms. Performance Tip 6.1 Avoid Fibonacci-style recursive programs, which result in an exponential explosion of method calls. 6.16 Recursion vs. Iteration In the previous sections, we studied two methods that can be implemented either recursively or iteratively. In this section, we compare the two approaches and discuss why the programmer might choose one approach over the other. Both iteration and recursion are based on a control structure iteration uses a repetition structure (such as for, whileor do/while) and recursion uses a selection structure (such as if, if/elseor switch). Both iteration and recursion involve repetition iteration explicitly uses a repetition structure and recursion achieves repetition through repeated method calls. Iteration and recursion each involve a termination test iteration terminates when the loop-continuation condition fails and recursion terminates when a base case is recognized. Iteration with counter-controlled repetition and recursion both gradually approach termination iteration keeps modifying a counter until the counter assumes a value that makes the loop-continuation condition fail and recursion keeps producing simpler versions of the original problem until a base case is reached. Both iteration and recursion can execute infinitely an infinite loop occurs with iteration if the loop-continuation test never becomes false and infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on a base case. Recursion has disadvantages as well. It repeatedly invokes the mechanism, and consequently the overhead, of method calls. This can be costly in both processor time and memory space. Each recursive call creates another copy of the method (actually, only the method s variables); this can consume considerable memory. Iteration normally occurs within a method, so the overhead of repeated method calls and extra memory assignment is omitted. Why then would a programmer choose recursion? Software Engineering Observation 6.11 Any problem that can be solved recursively also can be solved iteratively (nonrecursively). A recursive approach normally is chosen in preference to an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug. Recursive solutions also are chosen when iterative solutions are not apparent. Performance Tip 6.2 Avoid using recursion in performance situations. Recursive calls take time and consume additional memory.
You want to have a cheap webhost for your apache application, then check apache web hosting services.

Leave a Reply