I recently discovered the use of pure functions and subroutines in Fortran. From what the Fortran manual indicates, it seems that most of my subroutines can actually be defined as pure (since I always specify the intent of all the arguments, and usually I dont have "save", "pause", or external I/O in most of my subroutines). My question is then: Should I do it? I was wondering if the compiler optimizes better pure subroutines or if it just does not matter, or if it can make things worse. Thanks !
You work with the compiler to generate good code, and the more information you provide the compiler, the better a job the two of you can do together.
Whether it's labelling with intent(in)
any dummy arguments you don't change, or using parameter
for constants, or explicitly making pure
any subprogram which doesn't have any side effects, or using forall
when you don't really care about the order a loop is calculated in, by being more explicit about what you want to happen, you benefit because:
Of those three benefits, the optimization is probably not the most important; in the case of pure subroutines, a smart compiler can probably see just through static analysis that your subroutine has no side effects. Still, the more guarantees you can give it, the better a job it can do of optimizing your code while maintaining correctness.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With