This article is collected and collated by www.169it.com
In the STL Library of C + +, sorting can be done by saving all elements to the vector, then sorting by the sort algorithm, or by Multimap implementation when inserting elements. When sorting by vector+sort, all elements need to be stored in the vector container first, and sort will need to be taken out of the list before sorting. The Multimap is implemented as a red-black tree, so the elements are sorted in the process of insertion. So which sort is faster?
Here's a test program:
|
if
(gettimeofday(&tv, NULL) != 0) return
0.0; return
tv.tv_sec + tv.tv_usec / 1000000.0; } struct Score { string name; double
score; bool
operator <( const
Score& right) const
{ return
score < right.score; } }; int
main( int
argc, char ** argv) { vector<Score> src; for
( int
i = 0; i < 10000000; i++) { int
num = rand (); char
buf[32]; sprintf (buf, "%d" , num); Score score = { buf, num }; src.push_back(score); } { double
stime = time (); vector<Score> res; for
(vector<Score>::const_iterator it = src.begin(); it != src.end(); ++it) { res.push_back(*it); } sort(res.begin(), res.end()); double
etime = time (); printf ( "vector: %f\n" , etime - stime); } { double
stime = time (); multiset<Score> res; for
(vector<Score>::const_iterator it = src.begin(); it != src.end(); ++it) { res.insert(*it); } double
etime = time (); printf ( "multiset: %f\n" , etime - stime); } return
0; } |
The result of the program operation is:
1 2 3 |
time vector 4.776060 multiset 10.761023 |
In this test, the vector+sort sort is much faster than the multiset (Multimap implementation-based multiset). Fast sorting is the fastest sorting algorithm in all currently known sorting algorithms, so it is faster than multiset based on heap ordering.
Before this test results, most people would have no doubt that the multiset sort would be faster. This is also for a reason, fast sorting speed, although fast, but in the actual operation, it requires a large number of copies of elements, its copy operation of the time complexity of O (Nlogn), and based on the red-black tree multiset in the process of sorting avoids the copy of the element. If the memory footprint of the element is larger, the multiset sort will be faster than Vector+sort. To test this result, the structure in the above test program is modified to:
struct
score {
string name1;
string name2;
string Name3;
string name4;
Double
score;
BOOL
operator < (
const
score& right)
Const
{
return
Score < Right.score;
}
};
Then recompile and run the test program with the test result:
1 2 3 |
time vector 12.955739 multiset 11.368364 |
The results of this test are consistent with our expectations.
In short, when we use the STL sorting algorithm, we need to make the appropriate choice according to the different element constructs, if all is the relatively simple element, then is suitable chooses the vector+sort to carry on the sorting, for the string composition occupies the larger space the complex element, should use the multiset. If the total number of sorted elements is relatively small, select either one.
STL vector+sort Sorting and multiset/multimap sorting comparison