Speed problem of traversing map and list, history maplist speed

Source: Internet
Author: User

Speed problem of traversing map and list, history maplist speed

Today, my colleague wrote the following code:

For (int I = 0; I <map_T.count (); I ++)
{
T * t = map_T.value (map_T.keys (). at (I ));
}

I commented that the speed was too slow (in fact, there was not much content, and the method used to traverse users could not be felt). The result was a heated discussion, at the same time, we also discussed the Traversal method of the linked list, and used the data in the group to describe how to use a "subscript" for the traversal chain table faster than the iterator.

So I also wrote some code for testing (the test code is very easy, so I won't paste it). The results show that, map uses keys to traverse values more than 10 times slower than directly using iterators, because every access to values with keys results in binary searches, when using the iterator for access, the time complexity is O (1) (I did not analyze the source code, guess), and it must be faster than using keys to index values one by one.

When I tested the linked list, I suddenly found that the list in STL didn't provide the subscript access function at all, because the worst time for accessing with the subscript is too complex. For example, when the length of the linked list is very large, when using subscript to access a knot in the middle, it must go through many steps of indirect access.

After saying this, my colleague said that the QList provided by Qt provides the QList function <T>: at (int), which is used for subscript access and is faster than the iterator, i'm not satisfied, I don't want to run a minute, so I did the test again. But the result is that the at function traversal is nearly twice faster than the iterator, so I analyzed the source code of the QList and found the following code:

struct Q_CORE_EXPORT QListData {
    struct Data {
        QtPrivate::RefCount ref;
        int alloc, begin, end;
        void *array[1];
    };
    Data *d;
    inline void **at(int i) const { return d->array + d->begin + i; }
    inline void **begin() const { return d->array + d->begin; }

inline void **end() const { return d->array + d->end; }
  /// .........................
};
A good lesson.
It is estimated that Qt uses continuous memory to increase the speed of random access. It seems that we cannot compare it with STL, which is familiar with other libraries, after all, some databases are optimized and cannot solidify their knowledge.

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.