You know the answer you get, right? "Time is it."
There is probably no definitive answer. Firstly, the compiler can do the optimization for you. Secondly, even if it is not, indirect addressing at the assembly level may not be much slower. Thirdly, it depends on the cost of creating a local copy compared to the number of iterations of the loop. Then there are caching effects to consider.
I love to optimize, but this is one place that I will definitely wait until you have a problem, and then experiment. This is a possible optimization, which can be added if necessary, and not one of those optimizations that must be planned in advance to avoid the effect of massive ripple later.
Edit: (to final answer)
Compiling both functions in release mode and checking IL with IL Dasm shows that in both places the PossiblyFaster function uses a local variable, it has one more instruction
ldloc.0 vs
ldarg.0; ldfld class Constraint[] Manager::mConstraints
Of course, this is another layer removed from machine code - you don't know what the JIT compiler will do for you. But it is likely that "Probably Faster" will be a little faster.
However, I still do not recommend adding an additional variable until you are sure that this function is the most expensive on your system.
Ashelly
source share