hamid attar
hamid attar

Reputation: 388

performance implications with the result keyword in naming function return values

I am writing a code for high-performance computing. I found it handy to use the result keyword for naming a function's return value. I wonder if there is any performance issue here? In particular, would compilers make a temporary variable for the indicated 'result-name' to be copied eventually in the variable associated with the function name, or is the 'result-name' just an alias?

I compiled the following two codes with 'gfortran -S':

program test_result
  real*8 :: a, b, c
  a = 10.0
  b = 20.0
  c = myfunc(a, b)
contains
  function myfunc(x, y)
    real*8 :: myfunc
    real*8 :: x, y
    myfunc = x * y
  end function myfunc
end program test_result
program test_result
  real*8 :: a, b, c
  a = 10.0
  b = 20.0
  c = myfunc(a, b)
contains
  function myfunc(x, y) result(f)
    real*8 :: f
    real*8 :: x, y
    f = x * y
  end function myfunc
end program test_result

without optimization the compiler gave me two different assembly outputs, with optimization though the resulting assembly lines look the same. Does anybody know the implications here?

Thanks!

Upvotes: 1

Views: 139

Answers (1)

No there is not any performance penalty, it is just a syntactic issue, the generated code is identical. It is really just an "alias".

There should not be any differences even with optimizations disabled. My test confirms that. If it is not the case for you, you should show the differences you get from your compiler.

Upvotes: 3

Related Questions