forked from boostorg/move
Added adaptive sort test and refactored utilities between adaptive benches and the new test
This commit is contained in:
1 parent
499c5932bf
commit
baa5f0bbe7
6 files changed
+412
-230
No files matched your search
+48
-116
@@ -23,58 +23,15 @@ using boost::timer::cpu_timer;
|
||||
using boost::timer::cpu_times;
|
||||
using boost::timer::nanosecond_type;
|
||||
|
||||
|
||||
boost::ulong_long_type num_copy;
|
||||
boost::ulong_long_type num_elements;
|
||||
|
||||
struct sorted_type
|
||||
{
|
||||
public:
|
||||
std::size_t key;
|
||||
std::size_t val;
|
||||
|
||||
sorted_type()
|
||||
{
|
||||
++num_elements;
|
||||
}
|
||||
|
||||
sorted_type(const sorted_type& other)
|
||||
: key(other.key), val(other.val)
|
||||
{
|
||||
++num_elements;
|
||||
++num_copy;
|
||||
}
|
||||
|
||||
sorted_type & operator=(const sorted_type& other)
|
||||
{
|
||||
++num_copy;
|
||||
key = other.key;
|
||||
val = other.val;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~sorted_type ()
|
||||
{
|
||||
--num_elements;
|
||||
}
|
||||
};
|
||||
|
||||
boost::ulong_long_type num_compare;
|
||||
#include "order_type.hpp"
|
||||
|
||||
//#define BOOST_MOVE_ADAPTIVE_SORT_STATS
|
||||
void print_stats(const char *str, boost::ulong_long_type element_count)
|
||||
{
|
||||
std::printf("%sCmp:%7.03f Cpy:%8.03f\n", str, double(num_compare)/element_count, double(num_copy)/element_count );
|
||||
std::printf("%sCmp:%7.03f Cpy:%8.03f\n", str, double(order_type::num_compare)/element_count, double(order_type::num_copy)/element_count );
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
struct counted_less
|
||||
{
|
||||
bool operator()(const T &a,T const &b) const
|
||||
{ ++num_compare; return a.key < b.key; }
|
||||
};
|
||||
|
||||
#include <boost/move/algo/adaptive_sort.hpp>
|
||||
#include <boost/move/algo/detail/merge_sort.hpp>
|
||||
#include <boost/move/algo/detail/bufferless_merge_sort.hpp>
|
||||
@@ -99,22 +56,6 @@ void generate_elements(T elements[], std::size_t element_count, std::size_t key_
|
||||
}
|
||||
}
|
||||
|
||||
template<class T>
|
||||
bool test_order(T *elements, std::size_t element_count, bool stable = true)
|
||||
{
|
||||
for(std::size_t i = 1; i < element_count; ++i){
|
||||
if(counted_less<T>()(elements[i], elements[i-1])){
|
||||
std::printf("\n Ord KO !!!!");
|
||||
return false;
|
||||
}
|
||||
if( stable && !(counted_less<T>()(elements[i-1], elements[i])) && (elements[i-1].val > elements[i].val) ){
|
||||
std::printf("\n Stb KO !!!! ");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class T, class Compare>
|
||||
void adaptive_sort_buffered(T *elements, std::size_t element_count, Compare comp, std::size_t BufLen)
|
||||
{
|
||||
@@ -164,60 +105,60 @@ bool measure_algo(T *elements, std::size_t key_reps[], std::size_t element_count
|
||||
generate_elements(elements, element_count, key_reps, key_len);
|
||||
|
||||
std::printf("%s ", AlgoNames[alg]);
|
||||
num_compare=0;
|
||||
num_copy=0;
|
||||
num_elements = element_count;
|
||||
order_type::num_compare=0;
|
||||
order_type::num_copy=0;
|
||||
order_type::num_elements = element_count;
|
||||
cpu_timer timer;
|
||||
timer.resume();
|
||||
switch(alg)
|
||||
{
|
||||
case MergeSort:
|
||||
merge_sort_buffered(elements, element_count, counted_less<T>());
|
||||
merge_sort_buffered(elements, element_count, order_type_less<T>());
|
||||
break;
|
||||
case StableSort:
|
||||
std::stable_sort(elements,elements+element_count,counted_less<T>());
|
||||
std::stable_sort(elements,elements+element_count,order_type_less<T>());
|
||||
break;
|
||||
case AdaptiveSort:
|
||||
boost::movelib::adaptive_sort(elements, elements+element_count, counted_less<T>());
|
||||
boost::movelib::adaptive_sort(elements, elements+element_count, order_type_less<T>());
|
||||
break;
|
||||
case SqrtHAdaptiveSort:
|
||||
adaptive_sort_buffered( elements, element_count, counted_less<T>()
|
||||
adaptive_sort_buffered( elements, element_count, order_type_less<T>()
|
||||
, boost::movelib::detail_adaptive::ceil_sqrt_multiple(element_count)/2+1);
|
||||
break;
|
||||
case SqrtAdaptiveSort:
|
||||
adaptive_sort_buffered( elements, element_count, counted_less<T>()
|
||||
adaptive_sort_buffered( elements, element_count, order_type_less<T>()
|
||||
, boost::movelib::detail_adaptive::ceil_sqrt_multiple(element_count));
|
||||
break;
|
||||
case Sqrt2AdaptiveSort:
|
||||
adaptive_sort_buffered( elements, element_count, counted_less<T>()
|
||||
adaptive_sort_buffered( elements, element_count, order_type_less<T>()
|
||||
, 2*boost::movelib::detail_adaptive::ceil_sqrt_multiple(element_count));
|
||||
break;
|
||||
case QuartAdaptiveSort:
|
||||
adaptive_sort_buffered( elements, element_count, counted_less<T>()
|
||||
adaptive_sort_buffered( elements, element_count, order_type_less<T>()
|
||||
, (element_count-1)/4+1);
|
||||
break;
|
||||
case NoBufMergeSort:
|
||||
boost::movelib::bufferless_merge_sort(elements, elements+element_count, counted_less<T>());
|
||||
boost::movelib::bufferless_merge_sort(elements, elements+element_count, order_type_less<T>());
|
||||
break;
|
||||
case SlowStableSort:
|
||||
boost::movelib::detail_adaptive::slow_stable_sort(elements, elements+element_count, counted_less<T>());
|
||||
boost::movelib::detail_adaptive::slow_stable_sort(elements, elements+element_count, order_type_less<T>());
|
||||
break;
|
||||
case HeapSort:
|
||||
std::make_heap(elements, elements+element_count, counted_less<T>());
|
||||
std::sort_heap(elements, elements+element_count, counted_less<T>());
|
||||
std::make_heap(elements, elements+element_count, order_type_less<T>());
|
||||
std::sort_heap(elements, elements+element_count, order_type_less<T>());
|
||||
break;
|
||||
}
|
||||
timer.stop();
|
||||
|
||||
if(num_elements == element_count){
|
||||
if(order_type::num_elements == element_count){
|
||||
std::printf(" Tmp Ok ");
|
||||
} else{
|
||||
std::printf(" Tmp KO ");
|
||||
}
|
||||
nanosecond_type new_clock = timer.elapsed().wall;
|
||||
|
||||
//std::cout << "Cmp:" << num_compare << " Cpy:" << num_copy; //for old compilers without ll size argument
|
||||
std::printf("Cmp:%7.03f Cpy:%8.03f", double(num_compare)/element_count, double(num_copy)/element_count );
|
||||
//std::cout << "Cmp:" << order_type::num_compare << " Cpy:" << order_type::num_copy; //for old compilers without ll size argument
|
||||
std::printf("Cmp:%7.03f Cpy:%8.03f", double(order_type::num_compare)/element_count, double(order_type::num_copy)/element_count );
|
||||
|
||||
double time = double(new_clock);
|
||||
|
||||
@@ -240,7 +181,7 @@ bool measure_algo(T *elements, std::size_t key_reps[], std::size_t element_count
|
||||
, units
|
||||
, prev_clock ? double(new_clock)/double(prev_clock): 1.0);
|
||||
prev_clock = new_clock;
|
||||
bool res = test_order(elements, element_count, alg != HeapSort && alg != NoBufMergeSort);
|
||||
bool res = is_order_type_ordered(elements, element_count, alg != HeapSort && alg != NoBufMergeSort);
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -294,58 +235,49 @@ bool measure_all(std::size_t L, std::size_t NK)
|
||||
//Undef it to run the long test
|
||||
#define BENCH_SORT_SHORT
|
||||
|
||||
struct less
|
||||
{
|
||||
template<class T, class U>
|
||||
bool operator()(const T &t, const U &u)
|
||||
{ return t < u; }
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
measure_all<sorted_type>(101,1);
|
||||
measure_all<sorted_type>(101,7);
|
||||
measure_all<sorted_type>(101,31);
|
||||
measure_all<sorted_type>(101,0);
|
||||
measure_all<order_type>(101,1);
|
||||
measure_all<order_type>(101,7);
|
||||
measure_all<order_type>(101,31);
|
||||
measure_all<order_type>(101,0);
|
||||
|
||||
//
|
||||
measure_all<sorted_type>(1101,1);
|
||||
measure_all<sorted_type>(1001,7);
|
||||
measure_all<sorted_type>(1001,31);
|
||||
measure_all<sorted_type>(1001,127);
|
||||
measure_all<sorted_type>(1001,511);
|
||||
measure_all<sorted_type>(1001,0);
|
||||
measure_all<order_type>(1101,1);
|
||||
measure_all<order_type>(1001,7);
|
||||
measure_all<order_type>(1001,31);
|
||||
measure_all<order_type>(1001,127);
|
||||
measure_all<order_type>(1001,511);
|
||||
measure_all<order_type>(1001,0);
|
||||
//
|
||||
#ifndef BENCH_SORT_SHORT
|
||||
measure_all<sorted_type>(10001,65);
|
||||
measure_all<sorted_type>(10001,255);
|
||||
measure_all<sorted_type>(10001,1023);
|
||||
measure_all<sorted_type>(10001,4095);
|
||||
measure_all<sorted_type>(10001,0);
|
||||
measure_all<order_type>(10001,65);
|
||||
measure_all<order_type>(10001,255);
|
||||
measure_all<order_type>(10001,1023);
|
||||
measure_all<order_type>(10001,4095);
|
||||
measure_all<order_type>(10001,0);
|
||||
|
||||
//
|
||||
measure_all<sorted_type>(100001,511);
|
||||
measure_all<sorted_type>(100001,2047);
|
||||
measure_all<sorted_type>(100001,8191);
|
||||
measure_all<sorted_type>(100001,32767);
|
||||
measure_all<sorted_type>(100001,0);
|
||||
measure_all<order_type>(100001,511);
|
||||
measure_all<order_type>(100001,2047);
|
||||
measure_all<order_type>(100001,8191);
|
||||
measure_all<order_type>(100001,32767);
|
||||
measure_all<order_type>(100001,0);
|
||||
|
||||
//
|
||||
#ifdef NDEBUG
|
||||
measure_all<sorted_type>(1000001,1);
|
||||
measure_all<sorted_type>(1000001,1024);
|
||||
measure_all<sorted_type>(1000001,32768);
|
||||
measure_all<sorted_type>(1000001,524287);
|
||||
measure_all<sorted_type>(1000001,0);
|
||||
measure_all<sorted_type>(1500001,0);
|
||||
//measure_all<sorted_type>(10000001,0);
|
||||
measure_all<order_type>(1000001,1);
|
||||
measure_all<order_type>(1000001,1024);
|
||||
measure_all<order_type>(1000001,32768);
|
||||
measure_all<order_type>(1000001,524287);
|
||||
measure_all<order_type>(1000001,0);
|
||||
measure_all<order_type>(1500001,0);
|
||||
//measure_all<order_type>(10000001,0);
|
||||
#endif //NDEBUG
|
||||
|
||||
#endif //#ifndef BENCH_SORT_SHORT
|
||||
|
||||
//measure_all<sorted_type>(100000001,0);
|
||||
//measure_all<order_type>(100000001,0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user