问题
Is there any reason to believe that a fortran internal function would perform better than an external function?
e.g.
subroutine foo(x,y)
real :: x
x = bar(y)
return
contains
real function bar(x)
real :: x
bar = x*x
return
end function bar
end subroutine foo
vs
subroutine foo(x,y)
real :: x
real :: bar
x = bar(y)
return
end subroutine foo
real function bar(x)
real :: x
bar = x*x
return
end function bar
For example, does the internal unit allow the compiler to inline that code like some sort of macro?
回答1:
I don't believe that there is any general reason to believe that a Fortran internal function would perform better than an external function. By general reason I mean one which applies to all Fortran-standard-compliant compilers on all platforms. So I don't believe that one can articulate a general (in the same sense) preference for internal over external functions on performance grounds.
I'll go further, I don't believe that there is very much in the language's standard (any of them) that gives good support for arguments about the relative performance of variant approaches to implementing the same functionality. So, to take another example, I don't think that the standard would allow one to argue that an explicit do
loop ought to outperform an equivalent array-syntax-based expression. Or to argue to the contrary either.
But I do believe that there may be reasons, specific to a compiler version and/or a platform, for preferring, on performance grounds, one approach over another. For example, last time I tested the Intel Fortran compiler (probably v11.something) on MS Windows on an Intel processor, I concluded that explicit do
loops generally out-perform the equivalent array-syntax expressions.
Since performance is often a key concern of Fortran programmers, it behoves us to monitor the performance of various approaches as compilers develop and not to get stuck with ideas on performance which are out-dated.
As always in this game, hard data trumps argumentation. Program performance is an experimental science.
回答2:
For a possibly contentious spin on things... there is an implication that internal procedures "perform" better than external procedures in the standard, but not in the sense that you (probably) meant.
Internal procedures and module procedures get automatic explicit interfaces. This requires the compiler to check some aspects of each procedure reference, and enables most half decent compilers to check many other aspects. Achieving the same level of checks with an external procedure requires manual provision of an interface, which is error prone.
In the general case, the program robustness benefit (very much a form of performance) that arises from the automatic explicit interface obliterates any "speed of execution" performance considerations, that these days at best come down to "it depends"/"you'd need to measure to find out"/"there's probably no difference" for any decent compiler.
Having bar (and foo) as a module procedure in this case may go one better still - in that it eliminates unintended host association of variables with foo and enables independent testing of bar.
Selection of the type of procedure should be based on the technical requirements of the source code (valid reasons for writing external procedures remain today in modern Fortran code bases, but they are atypical), followed by the trade-off between robustness and clarity of the resulting source code that may be offered by module versus internal procedures. If you need to think about speed-of-execution performance in this decision, then your compiler is broken.
来源:https://stackoverflow.com/questions/14405779/performance-of-internal-function