The most tragic function in the array's left-rotated k-C ++ standard Algorithm Library: rotate

Source: Internet
Author: User

To rotate all elements of an array to the left k bit, there are usually three algorithms:

 

Algorithm 1 (group switching ):

If the length of a is greater than B, AB is divided into a0a1b, a0 and B are exchanged, and ba1a0 is obtained. Only a1 and a0 are exchanged.

If the length of a is less than B, AB is divided into ab0b1, and a and b0 are exchanged for b0ab1. Only a and b1 are exchanged.

Divide and exchange arrays until they cannot be further divided. The grouping process is similar to the maximum number of common appointments.

Read/write memory n to 2 * n times each

 

Algorithm 2 (cubic inversion)

Use ba = (br) r (ar) r = (arbr) r to reverse a and B respectively, and then reverse all elements once.

The read/write memory is about 2 * n times each.

 

Algorithm 3 (using loop chain)

Assume that the maximum common divisor of n and k is M, then all sequence numbers are (I + j * k) % n (0 <= I <M, 0 <= j <n/M) the elements constitute M cycle chains (The I value is the same on the same cycle chain), the elements on each cycle chain move to the element of the previous element, it can be switched to the location on the final result, so as long as the total read/write memory each n times. (For example, 1 2 3 4 5 6, 2 places left shift, 1 3 5 and 2 4 6 constitute two cycle chains respectively .)

 

In fact, the C ++ standard Algorithm Library provides a ready-made function: the rotate function. It is reasonable to say that several algorithms are relatively simple, and the library functions of the compiler are tested by time. The efficiency is not much worse even than that of handwriting. However, if you test the rotate function, you may find that the version of the standard library is not a little slow.

For VC 2010, run the subsequent test program. The custom function (using algorithm 2) takes 99 ms, while the std: rotate takes 1656 ms. Is the real-time library do not know how to use this simple algorithm? Check the source code of the lower database and you will find that three iterators (Forward iterator, bidirectional iterator, and random access iterator) of C ++ are in the standard algorithm library ), the above three algorithms are used respectively. Directly call its internal implementation (std: _ Rotat function) and retest it. The following result is displayed:

 

Iterator forward (algorithm 1) bidirectional (algorithm 2) Random Access (algorithm 3)
 
Time (MS) 46 99 1651
 

(If GCC is used, use versions earlier than 4.5 for testing)

 

We can see from the results that the efficiency is: algorithm 1> algorithm 2> algorithm 3.

 

Theoretically, algorithm 3 should be the most efficient algorithm as long as it reads and writes the memory n times each. This is true when the overhead of each memory read/write is not much different. However, due to hardware restrictions, the CPU uses a Hierarchical Cache Mechanism for memory access: the first-level cache capacity is small, but the access speed is the fastest. It stores program instructions and the most commonly used data, the second and third-level cache capacity is large, but the access speed is much slower. The CPU cannot bypass the cache to directly access memory data (some special commands do not use level-1 or level-3 caches, but they also need to use other dedicated caches, it must be first loaded into the cache. This operation is very expensive. For a large array, it is impossible to store all the data in the cache, but the memory access is not sequential, And the CPU overhead for memory location (the adjustment of data between cache levels, repeated data transfer or removal to the cache is huge, which causes the performance of algorithm 3 to be very poor. Test shows that when k = 3, the efficiency of the algorithm is quite poor. For small arrays, although the algorithm reads and writes a small number of times, it is difficult to reflect this advantage because the time used by various algorithms is very small. It can be said that algorithm 3 is very beautiful in mathematics, but it is a very poor Algorithm in practical application.

The memory factor should not be ignored for algorithm selection. This mistake was made in the roate Implementation of the random access iterator version, not just VC, but also the famous STL Port and GCC (GCC changed from 4.5 to libstdc ++ to algorithm 1, and made some optimizations), as well as the emerging libc ++. (Other compilers/libraries have never been used, and no tests have been performed .)

 

In addition, we found a bug in VC 2010 during the test: Implementation version of the forward iterator. When k = 0, the program went down directly.

 

 

Test code:
Rotate

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.