Will many parameters in a recursive function cause performance issues?
c++, c++11
Solution
The process during recursion is:
- Allocate space for parameters on the stack. Usually subtracting a value from the stack pointer register.
- Copy variable values onto the stack. Depends on the objects or values.
- Call function. This may cause a flush of the processor's instruction cache.
- At end of function, stack pointer is restored by adding a value.
- Return from function call; may cause flush to instruction cache.
The general concern is not performance, but recursion depth and stack size. Recursion that goes beyond the limitations of the stack is called a Stack Overflow defect.
An iterative solution may be faster because the compiler may be able to optimize the loop. Optimizing recursive calls is more difficult for a compiler to optimize.
By the way, on modern processors, the worst case timing of a recursive call is less than 1 millisecond, usually around nanosecond resolution. So you are trying to squeeze nanoseconds out of a program. Not very good Return On Investment (ROI).
Problem
My code will traverse around in a binary tree in a recursive fashion. Doing this I have some parameters I need to control. Thus, my function looks like this: ``` FindPoints(int leftchild, int rightchild, int ly_index, int uy_index, int bit, int nodepos, int amount, int level); ``` It is called a lot of times. Will the performance of my program take a hit because of the amount of parameters?