Reputation: 439
I'm implementing a new feature which might lead to quite a few subroutines along a certain code path being called recursively.
Therefore I have to assign the recursive
attribute to those subroutines.
My question is: How does this affect the compute time of those subroutines? Performance is very important in that code so it would be good to know any possible effects this might have.
Upvotes: 2
Views: 304
Reputation: 37228
The "proper" answer is to profile your code and test.
As a rule of thumb, how code generation is affected by the recursive keyword in contemporary compilers is mostly about large local arrays. For non-recursive procedures, these can be put in the static data section (.data or.bss, depending on the binary format of your platform), but this obviously doesn't work for procedures which might be called recursively. So in that case the compiler must create those variables on the heap where allocation and freeing can be costly, or just create them on the stack in any case and hope the user environment doesn't have a very small stack size limit.
As an aside, in the drafts for the upcoming Fortran 2015 standard, recursive procedures have been made the default, and there is a new keyword "non_recursive" if one explicitly wants to have a procedure which should not be called recursively.
Upvotes: 6