Object-oriented language C 3

Transfer from: http: //hi.baidu.com/todaygoodhujun/blog/item/4f6193ee453e1a292df53433.html

 

Simulation performed with Class C performance analysis
       classes used in the simulation of a large number of function pointers, structure, etc., we must have this performance analysis, such a structure in order to observe what extent influence the overall performance of the program.
 
1. Function call overhead
#define the COUNTER XX
void testFunc ()
{
       int I, K = 0;
       for (I = 0; I <YY; I ++) {K ++;}
}
 
       In the test program which, we are using a test function, the functions that can be changed by changing the time-consuming function of the value of YY. Test comparison is calling the cycle quadratic function XX YY cycle, and internal loop function XX times.
       It was found in the YY small enough, X is sufficiently large, time-consuming function calls has become the main reason. So when a "simple" requires "repeatedly" called time, it will be written as a function of an impact on performance. This time you can use macros or inline keyword.
       But, in fact I set XX = 10000000 (1 ten million) when, ms-level time-consuming to appear, for non-real-time operation (UI, etc.), even very slow cpu (embedded 10M level), also only a brief time-consuming function call in XX = 10 million for the time, so in fact this is negligible.
 
2. Normal function calls and function call overhead pointer
void (* TF) ();
TF = testFunc;
 
       Test program modified to use a function call, a function call using a pointer. Testing found that there is little impact on time. (The first time to write, it was found in the case of time-consuming function calls appear under (XX = 1 billion), the function pointer calls slower (release version), call takes 350: 500 and later discovered this effect is Since the global variable application of reason, access to global variables is much slower than a local variable ).
 
3. Function pointers and pointers to access the overhead structure
struct {A
       void (* TF) ();
};
 
       test modified to use a function pointer to a structure, the test found that substantially no effect on time. In fact, using the structure does not have an impact, because the access structure is a fixed offset. So access access structure variable and common variable for the machine code is the same.
 
Test Conclusion: Using class simulation approach will not have much impact on performance.

Reproduced in: https: //my.oschina.net/dake/blog/196671

Guess you like

Origin blog.csdn.net/weixin_33836874/article/details/91586202