I am pretty new to programming and using fortran 90 to write a cfd solver that takes a while to run. I am using a module and derived types to store global variables which are used in subroutines in a large do-loop in my main program. I have noticed that if I am referencing one of these global variables and don't need to alter its value, it is slower to use the variable defined in the module than to create a local variable within the subroutine. For instance, I have a type called grd with a real constant A stored in it. Instead of referencing grd%A over and over in a subroutine, it seems to be faster if I create a local A within the subroutine and assign it upfront, A=grd%A, and use A instead. I've run speed tests and for my particular program and defining this local A gives me about a 10% increase in speed, which ends up being pretty significant.
Can anyone explain why this would be the case? Are local variables always faster? Thanks!