It is more efficient to pass values to C ++ built-in function parameters.

Source: Internet
Author: User

In Objective C ++, it is mentioned that pass by value is more efficient for built-in (C-like) types when passing parameters in functions than pass by reference, when the OO c ++ custom type (constructor or destructor exist) pass by reference to const is better, the iterator and function objects in STL are implemented using the C pointer, therefore, pass by value is better. The following code verifies the cause.

# Include <iostream>
Using namespace std;

Int f (int I)
{
Int r = I + 1;
Return r;
}

Int g (const int & I)
{
Int r = I + 1;
Return r;
}

Int h (int * p)
{
Int r = * p + 1;
Return r;
}

Int inter (int * & p)
{
Int r = * p + 1;
Return r;
}

Int main ()
{
Int I = 0x11111111;
F (I );
G (I );
H (& I );

Int * x = & I;
R = inter (x );
Return 0;
}

The assembly code generated under the default Debug configuration of VS 2012 is as follows:

4: int f (int I)
5 :{
00F343D0 push ebp
00F343D1 mov ebp, esp
00F343D3 sub esp, 0CCh
00F343D9 push ebx
00F343DA push esi
00f34db push edi
00F343DC lea edi, [ebp-0CCh]
00F343E2 mov ecx, 33 h
00F343E7 mov eax, 0 CCCCCCCCh
00F343EC rep stos dword ptr es: [edi]
6: int r = I + 1;
00F343EE mov eax, dword ptr [I] // directly extract the I value to eax
00F343F1 add eax, 1 // eax + 1
00F343F4 mov dword ptr [r], eax
7: return r;
00F343F7 mov eax, dword ptr [r]
8 :}
00F343FA pop edi
00F343FB pop esi
00F343FC pop ebx
00F343FD mov esp, ebp
00F343FF pop ebp
00F34400 ret

The following functions only intercept key code.

10: int g (const int & I)
11 :{
......
12: int r = I + 1;
00F3449E mov eax, dword ptr [I] // same as passing a pointer, take the I address to eax
00F344A1 mov ecx, dword ptr [eax] // extract the eax value and put it in ecx.
00F344A3 add ecx, 1 // ecx value + 1
00F344A6 mov dword ptr [r], ecx
13: return r;
00F344A9 mov eax, dword ptr [r]
14 :}
......

Pass reference to pass pointer

16: int h (int * p)
17 :{
......
18: int r = * p + 1;
00F3453E mov eax, dword ptr [p] // get the address of p to make it in eax
00F34541 mov ecx, dword ptr [eax] // Obtain The eax value and make it in ecx.
00F34543 add ecx, 1 // exc value + 1
00F34546 mov dword ptr [r], ecx
19: return r;
00f341_mov eax, dword ptr [r]
20 :}
......

The pointer is the same as the above reference.

22: int inter (int * & p)
23 :{
......
24: int r = * p + 1;
01233DBE mov eax, dword ptr [p] // get the address of the passed parameter (pointer)-> eax
01233DC1 mov ecx, dword ptr [eax] // obtain the address of the parameter pointer --> ecx (the address of the real value)
01233DC3 mov edx, dword ptr [ecx] // Obtain the content of ecx-> edx
01233DC5 add edx, 1 // edx value + 1
01233DC8 mov dword ptr [r], edx
25: return r;
01233DCB mov eax, dword ptr [r]
26 :}
......

Pass by value


It can be seen from the assembly code that it is more efficient to transmit built-in types as function parameters.

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.