Visual C + + optimization Overview

Source: Internet
Author: User
Tags command line switches

Summary: Demonstrates several features in the many code optimization features provided by the Visual C + + 2003 compiler.

Microsoft Visual C + + Toolkit 2003 contains optimized C + + compilers. Most switches are fairly concise and are already present in multiple versions of the Visual C + + product, but there are still two switches that are newer and can significantly improve speed without rewriting the code. They are/GL (Whole program Optimization) and/G7 (which can produce code optimized for Pentium 4 or AMD Athlon). There is also an option/ARCH:SSE2, which produces code optimized for SSE2 registers and instructions.

The sample code passed three Tests:

1. Call functions that act as inline candidate functions.

2. Perform a large number of floating-point multiplication and addition operations.

3. Perform a large number of integer multiplication and addition operations.

Full program Optimization

The sample code defines two very similar functions: Add () and Displayadd (). Displayadd () is displayed to the screen, so it is unlikely to be inline:

void DisplayAdd(int a, int b)
{
  cout << a << " + " << b << " = " << a + b << endl;
  cout << "Return address from " << __FUNCTION__
    << " " << _ReturnAddress() << endl;
}

_returnaddress is an internal function that reports where control will be returned. You can use it to identify inline functions.

ADD () is declared in Gl-g7.cpp and also declares a global variable that is set by it:

void* inlineReturnAddress; // set in Add()
int Add(int a, int b); // implementation in module.cpp

Implementation is in Module.cpp:

int Add(int a, int b)
{
  inlineReturnAddress = _ReturnAddress();
  return a+b;
}

To compile the program and not use Whole Prgram optimization, use the following command line:

CL/O2/ML/EHSC Gl-g7.cpp Module.cpp

To run test 1, use the following command:

Gl-g7 1

You should see output similar to the following: The numeric address will be different.

1 + 2 = 3
Return address from DisplayAdd 00401D0A
1 + 2 = 3
Return address from Add 00401D13
Return address from Test1 00402125

The return address of Add () is different from the return address of Test1 (): Add () is not inline.

Now, recompile with/GL:

cl /O2 /ML /EHsc /GL GL-G7.cpp module.cpp

Run Test 1 again and you should see the output shown below:

1 + 2 = 3
Return address from DisplayAdd 00401242
1 + 2 = 3
Return address from Add 0040179F
Return address from Test1 0040179F

Now, add () and Test1 () return the same address: add () is inline within Test1 (), even if its code comes from another file.

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.