Learning point C language (39): functions-cost of using functions and inline functions)

Source: Internet
Author: User
C language is inseparable from functions, and it is convenient to use functions, but it is costly to use functions;
In particular, during repeated calls, the function will waste some time due to repeated pressure stacks and outbound stacks.

The following is a test. The time for using a function is 8 seconds and 4 seconds, respectively!

1. Test the cost of using the function:
# Include <stdio. h> # include <time. h> int sum (int x) {return x + X;} int main (void) {int COUNT = 1000000000; int I; int num; time_t start, stop; time (& START); for (I = 0; I

2. Use inline functions:

The Inline keyword on the function header is the inline function.

Using inline functions can optimize the functions. The so-called optimization is to embed the functions into the current position.

Generally, you can only consider using inline for small functions, because this is only an application, and the compiler does not guarantee to execute Inline for all applications.

Inline functions can only use units defined (rather than declared.

The above example (only one inline is added) is used for testing.

After several tests, the test results are basically the same as the above. It seems that C ++ builder 2009 does not care about it.

# Include <stdio. h> inline int sum (int x) {return x + X;} int main (void) {int COUNT = 1000000000; int I; int num; time_t start, stop; time (& START); for (I = 0; I

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.