std::stable_sort has some extra overhead in allocating the temp buffer,
which takes some time. The cutover point where it starts to get faster
than quicksort seems to be somewhere around 10 to 40 records.
So we're a bit conservative, and stay with quicksort up to 100 records.
*/
if (count <= 100)
{
if (param->sort_length < 10)
{
std::sort(m_sort_keys, m_sort_keys + count,
Mem_compare(param->sort_length));
return;
}
std::sort(m_sort_keys, m_sort_keys + count,
Mem_compare_longkey(param->sort_length));
return;
}
// Heuristics here: avoid function overhead call for short keys.
if (param->sort_length < 10)
{
std::stable_sort(m_sort_keys, m_sort_keys + count,
Mem_compare(param->sort_length));
return;
}
std::stable_sort(m_sort_keys, m_sort_keys + count,
Mem_compare_longkey(param->sort_length));
最后附上快速排序的代码
带排序数据是13,3,2,9,34,5,102,90,20,2
排序完成后如下:
gaopeng@bogon:~/datas$ ./a.out
sort result:2 2 3 5 9 13 20 34 90 102
/*************************************************************************