Originally Posted by
Kempelen
If a function uses global variables heavily, it is beneficial to copy those global variables into local variables so that they can be assigned to registers. This is possible only if those global variables are not used by any of the functions which are called.
Read that again... copy the globals, but don't use them?
Of course that also forgets the time wasted initially copying the variables to the stack... If you're going to be in that function for a long time and only need 2 or 3 ints from global sources it may catch up to itself, but one should reasonably think the time lost making copies of anything sizeable would outweigh the minor benefit of having them loaded into registers for 2 or 3 quicky operations.
That is an ideal world, I know, and i try it, but I use a vector of struct that is so long that can declare it local, but global, and accesed by pointers. That is the reason I ask my original question, it there are ways to optimize the use of that pointers.
About the only optimization from your original code sample would be to pass in a pointer directly to the answer and the values needed in the calculation...
Assuming int....
Code:
t_mytype *n; // Global n
void myfunc(int *result, int v1, int v2, int v3 ) {
*result = v1 + v2 +v3;
}
//called as...
myfunc(&n->result,n->var1,n->var2,n->var3);
That would effectively copy the variables to the stack (through the PUSH activity when entering the function) and it would place the result directly in your vector for you. Not only does this let the compiler do register optimization for you, it's a much better programming practice, even with global variables.