How big is the performance gap between std::sort and std::stable_sort in practice?

后端 未结 7 2074
我在风中等你
我在风中等你 2021-01-30 23:00

Both should run in O(n log n), but in general sort is faster than stable_sort. How big is the performance gap in practice? Do you have some experience about that?

I want

相关标签:
7条回答
  • 2021-01-30 23:23

    If you are sorting a large number of structs, the IO speed of your memory/disk starts to become more important than the asymptotic running time. Furthermore, memory usage should also be taken into consideration.

    I tried std::stable_sort on 2Gb of data (64B structs), not knowing that std::stable_sort creates an internal copy of the data. The swap trashing that followed almost locked up my pc.

    Using the unstable std::sort reduces memory usage by a factor of 2, which is useful when sorting large arrays. I terminated the std::stable_sort, so I cannot determine how much slower it was. However, if stable sort is not required, then I think it is better to use the unstable std::sort.

    0 讨论(0)
提交回复
热议问题