Optimizing program Performance (CSAPP:5)

Source: Internet
Author: User

"Foreword" Although there are no large projects, but work will pay attention to performance. Learning, should be able to better and faster understanding of other people write the classic good code. Combine Csapp and your own understanding to summarize.

I. Summary of program optimization

1, the characteristics of high-efficiency programs

(1) Appropriate algorithms and data structures. The organization of methods and data is undoubtedly the most critical and the basis of optimization;

(2) code can be translated into efficient executable code by the compiler. Need to know more about the compiler optimization method used, and the common optimization strategy;

(3) Using modern parallel programming technology. Multicore and hardware support provides greater acceleration possibilities, such as GPUs;

2, the general procedure of the optimization procedure

(1) Eliminate unnecessary work, such as eliminating unnecessary function calls (increasing the burden of the stack), condition testing and memory reference;

(2) Take advantage of the instruction-level parallelism provided by the processor, execute multiple instructions at the same time, the instruction level parallel between the thread-level concurrency and the single instruction multi-data parallel, a processor can achieve multi-instruction execution, such as pipelining technology;

(3) Optimize the critical path. Is the repeated execution of data and code;

Second, the ability and limitations of optimizing compilers

We can use-o1,-o2,-o3 to specify the compiler's optimization level, and the higher the level, the greater the size of the program. Note that the optimization level is generally better than the level of low performance, certainly better than the original not optimized! However, the compiler will consider security issues when optimizing, and if optimizations are guaranteed to be optimized in a safe range, a condition that is not satisfied will abandon the optimization of a module.

For example:

1 {2       *y + = *x; 3       *y + = *4}5// The above code will be optimized to the following?  6{7     2* *x; 8 }

No, although the general situation applies y=y+2x. However, when X=y: y=4x is not 3x. The compiler takes all special cases into account and ensures security. Another case is that memory aliases are used when two variable values are different, but the pointer address may be the same. is not optimized.

function calls can also hinder optimizations, which are now optimized with inline functions. Avoid frequent stack-in stacks.

Optimizing program Performance (CSAPP:5)

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.