Fastest way to sort a list of number and their index

前端 未结 8 1152
萌比男神i
萌比男神i 2021-02-14 21:15

I have a question that could seem very basic, but it is in a context where \"every CPU tick counts\" (this is a part of a larger algorithm that will be used on supercomputers).<

相关标签:
8条回答
  • 2021-02-14 21:44

    std::sort has proven to be faster than the old qsort because of the lack of indirection and the possibility of inlining critical operations.

    The implementations of std::sort are likely to be highly optimized and hard to beat, but not impossible. If your data is fixed length and short you might find Radix sort to be faster. Timsort is relatively new and has delivered good results for Python.

    You might keep the index array separate from the value array, but I think the extra level of indirection will prove to be a speed killer. Better to keep them together in a struct or std::pair.

    As always with any speed critical application, you must try some actual implementations and compare them to know for sure which is fastest.

    0 讨论(0)
  • 2021-02-14 21:55

    This will be used on supercomputers?

    In that case you may want to look into parallel sorting algorithms. That will only make sense for sorting large data sets, but the win if you need it is substantial.

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