[Reserved] cycle? ? burden? ?

xiaoxiao2021-03-06  26

The circulatory itself is also a big overhead, especially when the cycle itself is running fast. Some ways to avoid additional overhead are discussed here.

Cycling, not exaggerated, is that each programmer is encountered every day. As one of the three main structures of structured programming, the loop appears in almost every source of each software item. When one thing is placed in a loop, this matter may be made many times, which makes the loop are often optimal.

Few people think about it, the cycle itself is also a big overhead. Especially when the running speed of the circulation body itself is very fast. For example, the following functions may have been written:

INT Search {for (int = 0; i

This function is used to find the index value of the first integer equal to the given value in a integer array. If we write this function to the method of more direct (for the compiler), it may be

Int Expanded_Search (const INT * PI, INT ISIZE, INT IVALUE) {INT i = 0; goto judge; loop: IF (pi [i] == ivalue) Return I; Judge: i; if (i

Note that the cyclic code itself and the cyclic body have almost consumed the same time. That is, if we can avoid this loop mode (the cycle itself is impossible to avoid), we can get almost 100% speed improvement. Ronald E. Knuth mentioned in his masterpiece The Art of Computer Programming:

INT Guard_Search (int * pi, int xi, int tent {int * p = pi; p [isize] = iValue; while (* p! = ivalue) p; RETURN P == Pi ISIZE? -1: P - pi;

Compared to other methods, the only requirement is that an array for search is that one element must be empty. This is very easy to implement in the actual program, especially if necessary, in order to exchange execution speeds. As can be seen from the test results (as shown below), the optimization effect is quite significant.

Search: 380 Expanded_Search: 371 Guard_Search: 190

Sometimes, you have no way to achieve a guard variable, or you can't provide the last empty element. For example, the following functions:

Void Assign (int * pidest, const * pisrc, int isize) {for (int i = 0; i

In this case, the Knuth algorithm cannot be used. At this time, we can use Duff's Device ---- proposed an algorithm by DUFF. The modified function is as follows: void duff_assign (int * pIDest, const INT * PISRC, INT ISIZE) {INT i; switch (isize% 4) {case 0: for (i = 0; i

It can be seen that this function is very "ugly", the first look, even doubts whether it can be compiled. In fact, this is a "special" method for the C / C grammar. The main purpose is to reduce the jump and judgment in the cycle to obtain more specific gravity at each cycle.

The test results are as follows:

Do not use Duff's Device: 10235 Using Duff's Device: 9263

It has increased approximately 10%, although there are not many, but if you can make this simple change, you can improve the efficiency of the core code to improve, it is also worth it.

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

New Post(0)