First, the optimization of function call
Calling functions require multiple accesses to memory, so calls to functions are often time-consuming and prone to inefficient programs;
In the process of function call, if each function call result is the same and need multiple calls, the result of several calls can be accumulated multiple times, in order to avoid the inefficient function of multiple calls;
Second, variable storage optimization
For frequently used variables and only a certain range of variables, as far as possible to set it as a local variable, because the local variables are stored in the register, and the global variables are stored in the memory data segment, CPU access to memory is much lower than the access itself register. Compare:
Let's take a look at the first program:
int I;foo () { int a[]; for (i=0;i<; i++) a[i]=i;}
This code has to access the in-memory I for Each loop, so it's less efficient
Then look at the next program:
foo () { int i; int A[i]; for (i=0;i<; i++) a[i]=i;}
Since I is a local variable, it is stored on the register by the compiler and is read from the register every time I use it, so it is much more efficient than the previous one.
The function-related optimizations of C + +