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?
- IntMin (IntA,IntB)
- {
- ReturnA <B? A: B;
- }
- C = min (A, B );
- // Directly write
- 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?
- # Define min (A, B) (a) <(B ))? (A): (B)
- 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?
- Inline IntMin (IntA,IntB)
- {
- ReturnA <B? A: B;
- }
- C = min (A, B );
- // The compiler will optimize the code
- 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