struct structure increases program overhead

Source: Internet
Author: User

struct structure can strengthen the management of variables, increase the readability of the program, but on the other hand, the structure will increase the cost of the program.

Look at the following section of code:

struct Test_s{int a;int b;float c;}; int _tmain (int argc, _tchar* argv[]) {test_s ts;int a;int b;int c;ts.a = 100;ts.b = 200;TS.C = 300.f;a = 100;b = 200;c = 30 0.f;return 0;}
using the Disassembly tool,

<strong><span style= "FONT-SIZE:18PX;" ></span></strong><pre name= "code" class= "cpp" >TS.A = 100;ts.b = 200;TS.C = 300.F;

Assigning 3 variables to a struct, in fact the assembly code under debug is:

mov         dword ptr [TS],64H mov         dword ptr [ebp-0ch],0c8h  movss       xmm0,dword ptr ds:[0ba5858h]  movss       DWORD ptr [EBP-8],XMM0  
As you can see from the assembly code, in addition to the first variable in the struct, the next variable needs to be offset on the basis of the struct's header address, rather than the struct:

A = 100;b = 200;c = 300.F;
The assembly code is as follows:

mov         dword ptr [a],64h  mov         dword ptr [b],0c8h  mov         
significantly less one-step address subtraction.
However, in the case of release, the compiler optimizes the program by default, because the assembly code under optimization cannot be viewed, so the actual overhead is not ascertained.

struct structure increases program overhead

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.