- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The Intel(R) Fortran Compiler XE 13.0 User and Reference Guides states the following about the RESULT keyword:
However, if you use the RESULT keyword in a FUNCTION statement, you can specify a local variable name for the function result. In this case, all references to the function name are recursive calls, and the function name must not appear in specification statements.
My impression is that RECURSIVE routines are slower than non-RECURSIVE routines due to how variables may be allocated (the reason this was not allowed in F77). If my impression is correct, does that imply that whenever the RESULT keyword is used, there is an performance penalty? I use the RESULT keyword all the time as an alias for the function name, so that I can have descriptive function names (which tend to be relatively long) without having to use that name in the function body. Is this then a bad idea if performance is important?
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Next, the same program, slightly modified to use a RESULT clause: [fortran] program xrec1 implicit none integer :: n,fac n=5 fac=factorial(n) write(*,*)n,fac stop contains function factorial(n) result (fact) implicit none integer :: n, fact fact=1 if(n.le.1)return do while(n.gt.1) fact=fact*n write(*,*)n,fact n=n-1 end do return end function factorial end program xrec1 [/fortran] Again, note that no recursion occurs.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
is the compiler creating machine code as if the function was actually recursive?I'd expect it to, unless the optimizer is invoked and is smart enough to replace the simpler recursive code with a non-recursive version with the same functionality. Here is the assembler output from your function:
Even if the RECURSIVE attribute was present in a procedure that do not make any calls to other procedures (and hence cannot be recursive), will the compiler create code as if it actually were recursive?That would be up to the optimizer to decide. The Fortran standard does not specify such implementation details. The standard does specify in 12.6.5.2.1, w.r.t. the opposite question:
The RECURSIVE prefix-spec shall appear if any procedure defined by the subprogram directly or indirectly invokes itself or any other procedure defined by the subprogram.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Espen M. wrote:The short answer is "No". In fact, if you compile the two versions of the non-recursive function FACTORIAL above with the /Od /Fa options, you will find the assembler outputs to be identical. If you compile with debugging enabled, however, the symbol tables may differ slightly.Given a (non-recursive) function using a RESULT keyword, but without the RECURSIVE attribute. Will the machine code for this function differ from that of the same function programmed without using a RESULT keyword?
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page