//---------------------------------------------------------------------------- /// @file benchmark_numbers.cpp /// @brief Benchmark of several sort methods with integer objects /// /// @author Copyright (c) 2017 Francisco José Tapia (fjtapia@gmail.com )\n /// Distributed under the Boost Software License, Version 1.0.\n /// ( See accompanying file LICENSE_1_0.txt or copy at /// http://www.boost.org/LICENSE_1_0.txt ) /// /// This program use for comparison purposes, the Threading Building /// Blocks which license is the GNU General Public License, version 2 /// as published by the Free Software Foundation. /// /// @version 0.1 /// /// @remarks //----------------------------------------------------------------------------- #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "integer_sort.cpp" #include "ska_sort.hpp" #define NELEM 100000000 using namespace std; namespace bsort = boost::sort; namespace bsc = boost::sort::common; using bsc::time_point; using bsc::now; using bsc::subtract_time; using bsc::fill_vector_uint64; using bsc::write_file_uint64; using bsort::spinsort; using bsort::flat_stable_sort; using bsort::timsort; using bsort::spreadsort::spreadsort; using bsort::pdqsort; void Generator_random (void); void Generator_sorted (void); void Generator_sorted_end (size_t n_last); void Generator_sorted_middle (size_t n_last); void Generator_reverse_sorted (void); void Generator_reverse_sorted_end (size_t n_last); void Generator_reverse_sorted_middle (size_t n_last); template int Test (std::vector &B, compare comp = compare ()); int main (int argc, char *argv[]) { cout << "\n\n"; cout << "************************************************************\n"; cout << "** **\n"; cout << "** B O O S T S O R T **\n"; cout << "** S I N G L E T H R E A D **\n"; cout << "** I N T E G E R B E N C H M A R K **\n"; cout << "** **\n"; cout << "** SORT OF 100 000 000 NUMBERS OF 64 BITS **\n"; cout << "** **\n"; cout << "************************************************************\n"; cout << std::endl; //std::cout.flush(); //int code = system("lscpu"); //std::cout.flush(); //cout << "\n"; cout<<"[ 1 ] std::sort [ 2 ] pdqsort [ 3 ] std::stable_sort \n"; cout<<"[ 4 ] spin_sort [ 5 ] flat_stable_sort [ 6 ] timsort\n"; cout<<"[ 7 ] spreadsort [ 8 ] Fastest-Integer-S[ 9 ] ska_sort\n\n"; cout<<" | | | | | | | | | |\n"; cout<<" | [ 1 ]| [ 2 ]| [ 3 ]| [ 4 ]| [ 5 ]| [ 6 ]| [ 7 ]| [ 8 ]| [ 9 ]|\n"; cout<<"--------------------+------+------+------+------+------+------+------+------+------+\n"; std::string empty_line = " | | | | | | | | | |\n"; cout<<"random |"; Generator_random (); cout< A; A.reserve (NELEM); A.clear (); if (fill_vector_uint64 ("input.bin", A, NELEM) != 0) { std::cout << "Error in the input file\n"; return; }; Test> (A); } ; void Generator_sorted (void) { vector A; A.reserve (NELEM); A.clear (); for (size_t i = 0; i < NELEM; ++i) A.push_back (i); Test> (A); } void Generator_sorted_end (size_t n_last) { vector A; A.reserve (NELEM); A.clear (); if (fill_vector_uint64 ("input.bin", A, NELEM + n_last) != 0) { std::cout << "Error in the input file\n"; return; }; std::sort (A.begin (), A.begin () + NELEM); Test> (A); } ; void Generator_sorted_middle (size_t n_last) { vector A, B, C; A.reserve (NELEM); A.clear (); if (fill_vector_uint64 ("input.bin", A, NELEM + n_last) != 0) { std::cout << "Error in the input file\n"; return; }; for (size_t i = NELEM; i < A.size (); ++i) B.push_back (std::move (A[i])); A.resize ( NELEM); for (size_t i = 0; i < (NELEM >> 1); ++i) std::swap (A[i], A[NELEM - 1 - i]); std::sort (A.begin (), A.end ()); size_t step = NELEM / n_last + 1; size_t pos = 0; for (size_t i = 0; i < B.size (); ++i, pos += step) { C.push_back (B[i]); for (size_t k = 0; k < step; ++k) C.push_back (A[pos + k]); }; while (pos < A.size ()) C.push_back (A[pos++]); A = C; Test> (A); } ; void Generator_reverse_sorted (void) { vector A; A.reserve (NELEM); A.clear (); for (size_t i = NELEM; i > 0; --i) A.push_back (i); Test> (A); } void Generator_reverse_sorted_end (size_t n_last) { vector A; A.reserve (NELEM); A.clear (); if (fill_vector_uint64 ("input.bin", A, NELEM + n_last) != 0) { std::cout << "Error in the input file\n"; return; }; std::sort (A.begin (), A.begin () + NELEM); for (size_t i = 0; i < (NELEM >> 1); ++i) std::swap (A[i], A[NELEM - 1 - i]); Test> (A); } void Generator_reverse_sorted_middle (size_t n_last) { vector A, B, C; A.reserve (NELEM); A.clear (); if (fill_vector_uint64 ("input.bin", A, NELEM + n_last) != 0) { std::cout << "Error in the input file\n"; return; }; for (size_t i = NELEM; i < A.size (); ++i) B.push_back (std::move (A[i])); A.resize ( NELEM); for (size_t i = 0; i < (NELEM >> 1); ++i) std::swap (A[i], A[NELEM - 1 - i]); std::sort (A.begin (), A.end ()); size_t step = NELEM / n_last + 1; size_t pos = 0; for (size_t i = 0; i < B.size (); ++i, pos += step) { C.push_back (B[i]); for (size_t k = 0; k < step; ++k) C.push_back (A[pos + k]); }; while (pos < A.size ()) C.push_back (A[pos++]); A = C; Test> (A); }; template int Test (std::vector &B, compare comp) { //---------------------------- begin -------------------------------- double duration; time_point start, finish; std::vector A (B); std::vector V; //-------------------------------------------------------------------- A = B; start = now (); std::sort (A.begin (), A.end (), comp); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); pdqsort (A.begin (), A.end (), comp); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); std::stable_sort (A.begin (), A.end (), comp); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); spinsort(A.begin (), A.end (), comp); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); flat_stable_sort (A.begin (), A.end (), comp); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); timsort (A.begin (), A.end (), comp); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); spreadsort (A.begin (), A.end ()); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); ir_sort::integer_sort (A.begin (), A.end ()); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); A = B; start = now (); ska_sort (A.begin (), A.end ()); finish = now (); duration = subtract_time (finish, start); V.push_back (duration); //----------------------------------------------------------------------- // printing the vector //----------------------------------------------------------------------- std::cout<