手工编码的快速排序在较小的整数上比较慢
Hand-coded quicksort is slower on smaller integers
当比较我的快速排序实现与std::sort在我的编译器和我的归并排序实现时,我注意到一个奇怪的模式在大型数据集上:当操作64位整数时,快速排序始终比归并排序快;然而,对于较小的int大小,快速排序变慢,归并排序变快。
下面是测试代码:#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <utility>
#include <random>
#include <chrono>
#include <limits>
#include <functional>
#include <cstdint>
template <typename Iterator>
void insertion_sort(Iterator first, Iterator last)
{
using namespace std;
Iterator head = first;
Iterator new_position;
while(head != last)
{
new_position = head;
while(new_position != first && *new_position < *prev(new_position))
{
swap(*new_position, *prev(new_position));
--new_position;
}
++head;
}
}
template <typename Iterator>
void recursive_mergesort_impl(Iterator first, Iterator last, std::vector<typename Iterator::value_type>& temp)
{
if(last - first > 32)
{
auto middle = first + (last-first)/2;
recursive_mergesort_impl(first, middle, temp);
recursive_mergesort_impl(middle, last, temp);
auto last_merged = merge_move(first, middle, middle, last, temp.begin());
std::move(temp.begin(), last_merged, first);
}
else
{
insertion_sort(first, last);
}
}
template <typename Iterator>
void recursive_mergesort(Iterator first, Iterator last)
{
std::vector<typename Iterator::value_type> temp(last-first);
recursive_mergesort_impl(first, last, temp);
}
// Pick a pivot and move it to front of range
template <typename Iterator>
template <typename Iterator>
void quicksort_pivot_back(Iterator first, Iterator last)
{
using namespace std;
auto middle = first + (last-first)/2;
auto last_elem = prev(last);
Iterator pivot;
if(*first < *middle)
{
if(*middle < *last_elem)
pivot = middle;
else if(*first < *last_elem)
pivot = last_elem;
else
pivot = first;
}
else if(*first < *last_elem)
pivot = first;
else if(*middle < *last_elem)
pivot = last_elem;
else
pivot = middle;
swap(*last_elem, *pivot);
}
template <typename Iterator, typename Function>
std::pair<Iterator, Iterator> quicksort_partition(Iterator first, Iterator last, Function pivot_select)
{
using namespace std;
pivot_select(first, last);
auto pivot = prev(last);
auto bottom = first;
auto top = pivot;
while(bottom != top)
{
if(*bottom < *pivot) ++bottom;
else swap(*bottom, *--top);
}
swap(*pivot, *top++);
return make_pair(bottom, top);
}
template <typename Iterator>
void quicksort_loop(Iterator first, Iterator last)
{
using namespace std;
while(last - first > 32)
{
auto bounds = quicksort_partition(first, last, quicksort_pivot_back<Iterator>);
quicksort_loop(bounds.second, last);
last = bounds.first;
}
}
template <typename Iterator>
void quicksort(Iterator first, Iterator last)
{
quicksort_loop(first, last);
insertion_sort(first, last);
}
template <typename IntType = uint64_t, typename Duration = std::chrono::microseconds, typename Timer = std::chrono::high_resolution_clock, typename Function, typename Generator>
void run_trial(Function sort_func, Generator gen, std::string name, std::size_t trial_size, std::size_t trial_count)
{
using namespace std;
using namespace chrono;
vector<IntType> data(trial_size);
Duration elapsed(0);
cout << "Sorting with " << name << endl;
for(unsigned int i = 0; i < trial_count; ++i)
{
generate(data.begin(), data.end(), gen);
auto start = Timer::now();
sort_func(data.begin(), data.end());
auto finish = Timer::now();
elapsed += duration_cast<Duration>(finish-start);
}
cout << "Done. Average elapsed time: " << elapsed.count() / trial_count << endl;
cout << "Is correct: " << is_sorted(data.begin(), data.end()) << endl << endl;
}
int main()
{
using namespace std;
using namespace chrono;
using int_type = uint64_t;
const size_t trial_size = 12800000;
const int trial_count = 15;
vector<int_type> data(trial_size);
uniform_int_distribution<int_type> distr;
mt19937_64 rnd;
run_trial<int_type>(recursive_mergesort<vector<int_type>::iterator>, bind(distr, rnd), "recursive mergesort", trial_size, trial_count);
run_trial<int_type>(quicksort<vector<int_type>::iterator>, bind(distr, rnd), "quicksort", trial_size, trial_count);
run_trial<int_type>(sort<vector<int_type>::iterator>, bind(distr, rnd), "std::sort", trial_size, trial_count);
}
下面是对12800000个元素进行15次试验的次数:
uint64_t
:
Sorting with recursive mergesort
Done. Average elapsed time: 1725431
Is correct: 1
Sorting with quicksort
Done. Average elapsed time: 1238070
Is correct: 1
Sorting with std::sort
Done. Average elapsed time: 1131464
Is correct: 1
uint16_t
:
Sorting with recursive mergesort
Done. Average elapsed time: 1186467
Is correct: 1
Sorting with quicksort
Done. Average elapsed time: 2368535
Is correct: 1
Sorting with std::sort
Done. Average elapsed time: 888517
Is correct: 1
我有一种感觉,这个问题与未对齐的内存访问有关,然而,这仍然让我想知道为什么其他算法得到加速,而快速排序变慢。
使用uint16_t
,您将在如此大的数组中获得大量重复:在期望中,0到65535的每个值出现195次。如果没有一个三向("胖")分区,或者至少一个可以返回它正在处理的子数组中重复出现的枢轴值的中间的分区,将导致快速排序变成二次型。(在一个只有0的数组上用铅笔和纸来执行简单的快速排序,看看效果如何。)
相关文章:
- C++模板函数,用于比较任何无符号整数和有符号整数
- 正在LLVM中检测整数比较条件
- 为什么:不同符号的整数比较只是偶尔发生?
- C++ 如何比较n个排序的整数向量以找到互元素?
- 比较C++中两个整数的最有效和最干净的方法是什么?
- 禁止指针和整数之间的比较C++
- ISO C++禁止指针和整数 [-fpermissive] [c++] 之间的比较
- Qsort() 比较结构体整数的总和
- 比较两个整数在C++中与未知 int 类型的相等性
- Qt 错误 iso c++ 禁止指针和整数之间的比较 -permissive
- 比较整数提升后的结果
- 对压缩整数进行加法、减法和比较
- "正确"无符号整数比较
- C++,指向整数比较错误的指针
- 推力::max_element 比较慢 cublasIsamax - 更有效的实现
- C++指针与整数比较编译错误
- c++集比较慢
- 手工编码的快速排序在较小的整数上比较慢
- 为什么此指针/整数比较会生成分段错误
- 这是不可避免的有符号和无符号整数比较吗?