C ++ efficient Program Design

Source: Internet
Author: User

A recently developed program requires a high speed of code. At the same time, because the implemented code speed cannot meet the requirements, it searches. Add this article to favorites.

Source: http://www.kuqin.com/language/20090314/39898.html


Summary
Whether you are willing to admit it or not, everyone wants the program to run faster and better. Every day, people catch up with me, as if tomorrow is the last day. At the same time, the people in the Public Relations Department kept shouting, saying that their new engine is "faster" and "better" than others ".

I'm not going to tell you how to make your code run faster than others. I just want to tell you how to make your code faster and more efficient. Of course, it is compared with your original code.
My content mainly involves three concepts. The relationship between these three concepts is quite complex:
1. Code Execution time
2. Code/program size
3. Program Design expenditure
I always believe that the balance between the three should be maintained, especially in some cases, items 2 and 3 directly affect the code execution time.

In this article, I will introduce some methods that may help you improve code execution efficiency. I will start with the simplest optimization method, and then gradually go deep into those complicated technologies. Now let's start from a very inconspicuous place: the compiler.

Considering some experienced programmers in the reader, my narrative will be as simple as possible to avoid clutter due to too many details.

First, to do good deeds, we must first sharpen our tools.
The content in this section does not seem to be worth mentioning, but think carefully, how much do you know about the compiler in your hands? Do you know which processors it can generate code? Do you know what types of optimization it can perform? Do you know its language compatibility?
When you want to write something, especially when you want your code to run like a fly, it is crucial to understand this content.
For example, someone recently asked a question about Microsoft Visual C ++'s "Release Mode" in the GameDev discussion group. This is a standard compiler option. If you use a specific compiler, you should know what it means. If you do not know, it is a pity that you do not really use things you have spent a lot of money to buy. To put it simply, "Release Mode" will delete all debug code, optimize all possible compilation code, generate smaller executable files, and make the file run faster. It may have some other functions. If you are interested, please read the relevant documentation of the compiler.

Now, if you didn't know the "Release Mode" before, I can tell you a way to make your code run faster, you do not need to modify any code for this method!

The target platform is also very important. Now you may encounter the Intel Pentium processor, but if you use the compiler 10 years ago, it will not do any Optimization for Pentium. Find the latest compiler, which may greatly increase the running speed of the program. Similarly, you do not need to make any modifications to the code.

Also, pay attention to the following: Does your compiler have a profiling tool? If you don't even know this, you don't want to write faster code. If you do not know what code analysis tools are, you need to learn more. A code analysis tool is used to obtain the running time of a program. You can run your program in the profiler, perform some operations, and then exit from your program to get a report about the time consumption of each function. You can find the code running bottleneck based on this report-the most time-consuming part of your code. Some specific optimization for these parts is much better than simply doing some optimization in every place.

Don't say "but I know where the bottleneck is !" They can be found only in your mind, especially when using third-party APIs and libraries. A few weeks ago, I also encountered a similar problem. In a video program, the status switch is inexplicably generated when each frame is displayed, and this action takes 25% of the total execution time. By simply adding a test Statement (whether the test state has been set), I remove the corresponding function from the list of 50 most expensive functions obtained by analysis.

It seems that in most cases, analyzer can easily be used, but in fact it is not. You must find the key path in the program. The so-called critical path is the path where most of the program's running time is being executed. Optimizing the Key Path can significantly improve the running efficiency and your users will be happy with it.

In another case, you may find that the most time-consuming step in a function is to load a specific file, but you know that this will only happen once when the application starts. Optimization of this function may reduce the total running time of the program by several seconds, but it will not improve the efficiency of normal use. In fact, this indicates that you have not performed sufficient code analysis, because in normal use, the percentage of time used by this function will decrease, the percentage of time your key path takes will soar.

I think the above content will give you some knowledge about these tools.

The code analysis tool is really good. Remember to use it!

If you do not have a Code Analyzer, try Intel's VTune profiler. You can try it for free for one month. Download it at http://developer.intel.com/vtune/analyzer /.

In the next part of this article, I will show you how to make your C/C ++ compiler do what you want it to do.

Section 2 Inlining and inline keywords

What is inlining? I will answer this question by describing the inline keyword.

The Inline keyword tells the compiler to "expand a function in a proper place". It works like a macro (# define) in C and C ++, but it is a little different. The Inline function is type-safe, and its main function is to help the compiler optimize code. With it, you can have both macro speed (no extra overhead for function calling) and function type security, as well as a lot of other benefits.

What are the benefits? Most compilers can only optimize the code in one module at a time. It is usually a. h/. cpp file pair. Using the inline function enables the compiler to optimize the code of functions in different modules, such as eliminating the copy of return values and eliminating unnecessary temporary variables. If you want to know

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.