Why is std::list<>::sort so slow ??? A (much!) faster way to sort lists. #1650
-
The other day on stackoverflow, I helped a student with his implementation of bubblesort for a list. In doing so, I created a small project on godbolt with 3 different algorithms for sorting lists. I did some naive comparisons of performance and compared to their performance to std::list::sort(). The STL is slow. for smalll arrays, it is even slower than the simple algorithm I've been using ;fr ages, which is very simple and easy. Anyway, while comparing these algotithms, I implemented a naive partition based algorithm for sorting lists, bvery similar to qsort, and was surprised by the results. Around 90% improvement over std::list::sort for short lists, whiich was not surprising, but the improvements were consistent over a wide range, it's still stands at over 50% for a 10000 elements list. I know I'm not doing anything wrong... And I don't think I invented anything new. Is thre a specific reason why std::sort is so slow, You'll find my sandbox on godbolt here: https://godbolt.org/z/Y8eE1f Here is the sorting function:, [edit] I've added pivot selection for corner cases. Still up to 75% improvement (in corner case), even with 1,000,000 elements
m:o) |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
As far as I can tell, your |
Beta Was this translation helpful? Give feedback.
-
std::list::sort is stable. Is your version a stable sort? |
Beta Was this translation helpful? Give feedback.
-
I think implementations of If the requirement of stability were removed, we would be able to use the combination of quick sort and merge sort. |
Beta Was this translation helpful? Give feedback.
I think implementations of
std::(forward_)list::<>::sort
generally use merge sort. And merge sort is expected because the standard requires that thesesort
's are stable.If the requirement of stability were removed, we would be able to use the combination of quick sort and merge sort.