Performance Analysis of class simulation

Source: Internet
Author: User

Class simulation uses a large number of function pointers, struct, and so on. Performance analysis is required to observe the effect of such a structure on program integrity.

1. function call overhead

# Define COUNTER XX

Void testfunc ()

{

Int I, k = 0;

For (I = 0; I <YY; I ++) {k ++ ;}

}

In the test program, we use a test function. In the function body, we can change the time consumption of the function by changing the YY value. The test comparison is to call the XX function cyclically and the YY cycle inside the XX function cyclically.

The result shows that function call time consumption is the main reason when YY is small enough and X is large enough. Therefore, when a "simple" function requires "repeated" calls, writing it as a function will affect the performance. In this case, you can use macros or the inline keyword.

However, when I set XX = 10000000 (10 million), the ms-level time consumption occurs. For non-real-time operations (UI, etc ), even a very slow cpu (embedded 10 m level) Only takes a short time to call a function when XX = 0.1 million. Therefore, this can be ignored.

2. overhead of common function calls and function pointer calls

Void (* tf )();

Tf = testfunc;

The test program is changed to a function call and a function pointer call. The test shows that there is basically no effect on the time. (During the first compilation, it was found that when function calling is time-consuming (XX = 0.1 billion), function pointer calling is slow (release version), and the call time is 350: 500. Later, we found that the reason for applying for a global variable is that the access to global variables is much slower than that of local variables ).

3. overhead of function pointer and pointer structure access

Struct {

Void (* tf )();

};

The test program changes to a function pointer using the structure, and the test finds that there is basically no impact on time. In fact, the use of the structure does not affect, because the structure access is fixed offset. Therefore, access to structure variables is the same as access to common variables for machine codes.

Test conclusion: the use of the class simulation method will not have a great impact on the performance.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.