Which is faster? Recursion or Iteration? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Which is faster? Recursion or Iteration?

22nd Jun 2018, 11:15 AM
Terence
Terence - avatar
3 Answers
+ 19
Iteration
22nd Jun 2018, 11:21 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 6
Hello, Terence ! Your performance deteriorates when using recursion, because calling a method in any language involves a lot of preparation: the call code sends the return address, the call parameters, other information about the content, such as the processor registers, can be stored somewhere, and for the return time The called method sends the return value, which is then retrieved by the caller, and any context information that was previously stored will be restored. The difference between the iterative and recursive approach is that these operations are performed. In terms of implementation, you are really starting to notice the difference when the time required to process the calling context is comparable to the time it takes to execute your method. If your recursive method takes longer to execute, go in a recursive way, because the code is usually more readable and understandable.
22nd Jun 2018, 12:31 PM
Alexander Sokolov
Alexander Sokolov - avatar
+ 1
i think its ITERATION
23rd Jun 2018, 9:08 AM
Alex
Alex - avatar