Inline functions (from C ++ primer plus)

Source: Internet
Author: User

I heard an example about inline in the class about the efficiency of inline function execution. After class, I wrote a record based on my memories. It can be seen that the execution efficiency of inline functions is indeed higher than that of common functions.

Don't talk nonsense.Code:

Call the inline function:

# Include <iostream>
Using namespace STD;

Inline void delay ()
{
Int;
For (long int I = 0; I <10000; I ++)
A = 0;
}
Int main ()
{
For (long int I = 0; I <10000; I ++)
Delay ();
Cout <"which one is long? "<Endl;
Return 0;
}

Inline function not called

# Include <iostream>

Using namespace STD;

Void delay ()
{
Int;
For (long int I = 0; I <10000; I ++)
A = 0;
}

Int main ()
{
For (long int I = 0; I <10000; I ++)
Delay ();
Cout <"which one is long? "<Endl;
Return 0;
}

You can execute it in windows and observeProgramThe execution time is relatively long. We can see that the execution time of calling inline functions is significantly shorter than that of calling non-inline functions. Note that Win32 debug does not show the effect. The result can only be seen in Win32 release.

The following is an explanation from the perspective of C ++:

Inline functions are an improvement made by C ++ to improve program running efficiency. The main difference between conventional and inline functions is how the compiler combines them into a program.

The final product compiled is an executable program, that is, machine code. When running a program, the operating system loads these commands into the computer's memory, so each command has a specific address, and the computer executes these commands in order. In function calls and other operations, it involves skipping some commands on the computer, executing other commands forward or backward, and then jumping back, during this period, on-site protection and other out-of-stack operations will be involved, which takes a lot of time.

Inline functions provide another method of operation. The Compiler directly expands the place where inline functions are called, rather than using functions, this will save the inbound and outbound operations caused by program jumps, so the execution time is short, but it also brings a drawback: the code needs to occupy more memory.

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.