Program optimization content and principle

xiaoxiao2021-03-05  22

Program optimization content and principle

Optimization of the program, usually refers to the optimization program code or program execution speed. Optimizing code and optimization speed is actually a contradictory unity. It is generally an increase in code, which will increase the execution time. If the speed of the optimizer is executed, it usually brings the side effect of increasing the code, it is hard to fish The bear's palm is both, can only master a balance at design time.

If we really need to be substantially optimized to some code, then we must first know which part of the code's execution is the most wasteful. It is often the most wasteful code, most of which is a large number of cycles.

Optimized levels are often divided into three categories: algorithm optimization; language level optimization; command level optimization.

The most important place to reflect a programmer level is the algorithm. A good algorithm can use very little code to achieve the original complex operation, but it is difficult to do. Especially these algorithms are often related to the goodness of the load, so it is necessary to compare and test to have a good effect.

Language-level optimization is to use fewer program language code instead of lengthy code blocks, for example, put some assignment statements in the outer side of the multi-cycle, use the Inline function, use the pointer, use the reference instead of structure, use the movement of the pointer instead Memory copy, put the initialization operation at the beginning rather than the middle of the cycle, and so on. The principle it follow is "no code" principle, reducing the statement that needs to be executed is the most direct approach to the speed. Such a program is relatively simple and the operation is relatively stable.

The instruction level optimization is much more, and the language used here is generally assembly language. The debugging and testing of this method is more complicated, and the program is not easy to understand, and it is more likely to be wrong, and the results are sometimes related to hardware. The number of code for this method should be relatively small, only a key part. Such an optimization is in units of instruction cycle.

Optimized content is generally:

Code replacement

Use a short instruction instead of a period of instructions during cycle. For example, using the left shift instruction instead of multiplication of 2 multiples; uses a reciprocal instruction instead of the division instruction.

Reduce branch prediction

This is the unique function of Pentium or more, it will read some instructions before the instruction, but if there is a branch, it will cause pre-reading failure.

Parallel instruction

This is the advantage of Pentium's unique multi-street waterline, two (or more, above the Pentium Pro) parameter-independent instructions can be performed in parallel.

MMX instruction

It can be used when processing a large number of byte data, and can handle 8 bytes of data at a time.

Readup

When reading a lot of data, if the data is not in the cache, it will waste a lot of time, so you need to put the data in a cache in advance. This feature appears in the next-generation CPU of the Pentium II.

The principle of optimization has the following 3:

Equity Principle: The function of operation should not be changed after optimization.

Effective principle: Optimization The target code generated after optimization is indeed shorter, and the space occupied is indeed small.

Combined principles: It should be made as much as possible to achieve better optimization effects, and should be optimized for programs worth optimized.

转载请注明原文地址:https://www.9cbs.com/read-37524.html

New Post(0)