Write efficient code (7) Reduce function calls-do not interrupt me

Source: Internet
Author: User

Functions are the product of structured program design. They make the code more modular, have lower coupling, and have higher reusability. However, function calls bring additional overhead, in addition to causing redirection, and also generate additional commands.

People have such experience. If someone is interrupted and re-attempts to do this, it will take some time to recover. If they are always interrupted, then we can't do it. This is also the case for function calls, such as parameter pressure stack-out stack, register storage, and command jump. Multiple steps if the program has high performance requirements, you can directly convert some small functions into code.

1.Write a small function as a statement.

The following function for minimum value can be used to replace the function call with the function content.

[CPP]
View plaincopyprint?
  1. IntMin (IntA,IntB)
  2. {
  3. ReturnA <B? A: B;
  4. }
  5. C = min (A, B );
  6. // Directly write
  7. C = A <B? A: B;
Int min (int A, int B) {return a <B? A: B;} c = min (a, B); // write it as c = A <B? A: B;

 

2.Write a small function into a macro.

If there are many calls, the method of using the function call will look concise. One way is to keep the code concise and reduce function calls by declaring the function body as a macro.

[CPP]
View plaincopyprint?
  1. # Define min (A, B) (a) <(B ))? (A): (B)
  2. C = min (A, B );
#define min(a,b) ((a)<(b)) ? (a) : (b)c = min(a,b);

 

3.Declare a function as an inline function

If the macro method is too cumbersome, another simple method is to declare the function as inline, And the compiler will automatically overwrite the function call with the function body.

[CPP]
View plaincopyprint?
  1. Inline IntMin (IntA,IntB)
  2. {
  3. ReturnA <B? A: B;
  4. }
  5. C = min (A, B );
  6. // The compiler will optimize the code
  7. C = A <B? A: B;
Inline int min (int A, int B) {return a <B? A: B;} c = min (a, B); // the compiler will optimize the code to C = A <B? A: B;

This article is excerpted from Chapter 6th of "big talk processor.

To make a computer excellent, its hardware and software must be closely linked. -- Steve Jobs

Source: http://blog.csdn.net/muxiqingyang/article/details/7043476

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.