+ 1

Why are recursive functions considered slower than their iterative counterpart?

My question is from Recursion https://www.sololearn.com/discuss/1911868/?ref=app https://code.sololearn.com/cj0MOpnCtz7L/?ref=app

1st Aug 2019, 3:17 PM
Pragya Ajay Singh
Pragya Ajay Singh - avatar
2 odpowiedzi
+ 2
In general recursive functions are slower, though the difference is not significant. This happens simply because a function call is more "expensive" than iterative function, which uses loop. However, the difference of how much space it takes is quite significant, since every calls need to create a new variables, if the function have any, or at least a new return address, also this is one of the reason why it is more "expensive"
1st Aug 2019, 3:46 PM
Agent_I
Agent_I - avatar
+ 1
Recursive functions arn’t always slower than iterative functions. I would recommend googling it, theres probaly a good comparison out there
1st Aug 2019, 3:20 PM
Ole113
Ole113 - avatar