11/*
2- * Copyright (c) 2015-2021 Morwenn
2+ * Copyright (c) 2015-2022 Morwenn
33 * SPDX-License-Identifier: MIT
44 */
55#include < algorithm>
@@ -46,8 +46,10 @@ template<
4646 typename DistributionFunction
4747>
4848auto time_it (Sorter sorter, DistributionFunction distribution)
49- -> double
49+ -> std::uint64_t
5050{
51+ static_assert (N > 0 , " this benchmark does not support zero-sized arrays" );
52+
5153 // Seed the distribution manually to ensure that all algorithms
5254 // sort the same collections when there is randomness
5355 distributions_prng.seed (seed);
@@ -65,53 +67,53 @@ auto time_it(Sorter sorter, DistributionFunction distribution)
6567 sorter (arr);
6668 std::uint64_t end = rdtsc ();
6769 assert (std::is_sorted (arr.begin (), arr.end ()));
68- cycles.push_back (end - start);
70+ cycles.push_back (double ( end - start) / N );
6971 total_end = clock_type::now ();
7072 }
7173
72- // Return the average number of cycles it took to sort the arrays
73- std::uint64_t avg = 0 ;
74- for (auto value: cycles) {
75- avg += value;
76- }
77- return avg / double (cycles.size ());
74+ // Return the median number of cycles per element
75+ auto cycles_median = cycles.begin () + cycles.size () / 2 ;
76+ std::nth_element (cycles.begin (), cycles_median, cycles.end ());
77+ return *cycles_median;
7878}
7979
8080template <
8181 typename T,
82- typename Distribution ,
82+ typename Dist ,
8383 std::size_t ... Ind
8484>
8585auto time_distribution (std::index_sequence<Ind...>)
8686 -> void
8787{
88- using sorting_network_sorter = cppsort::small_array_adapter<
89- cppsort::sorting_network_sorter
90- >;
91-
9288 using low_comparisons_sorter = cppsort::small_array_adapter<
9389 cppsort::low_comparisons_sorter
9490 >;
95-
9691 using low_moves_sorter = cppsort::small_array_adapter<
9792 cppsort::low_moves_sorter
9893 >;
94+ using merge_exchange_network_sorter = cppsort::small_array_adapter<
95+ cppsort::merge_exchange_network_sorter
96+ >;
97+ using sorting_network_sorter = cppsort::small_array_adapter<
98+ cppsort::sorting_network_sorter
99+ >;
99100
100101 // Compute results for the different sorting algorithms
101- std::pair<const char *, std::array<double , sizeof ...(Ind)>> results[] = {
102- { " insertion_sorter" , { time_it<T, Ind>(cppsort::insertion_sort, Distribution{})... } },
103- { " selection_sorter" , { time_it<T, Ind>(cppsort::selection_sort, Distribution{})... } },
104- { " low_moves_sorter" , { time_it<T, Ind>(low_moves_sorter{}, Distribution{})... } },
105- { " low_comparisons_sorter" , { time_it<T, Ind>(low_comparisons_sorter{}, Distribution{})... } },
106- { " sorting_network_sorter" , { time_it<T, Ind>(sorting_network_sorter{}, Distribution{})... } },
102+ std::pair<const char *, std::array<std::uint64_t , sizeof ...(Ind)>> results[] = {
103+ { " insertion_sorter" , { time_it<T, Ind + 1 >(cppsort::insertion_sort, Dist{})... } },
104+ { " selection_sorter" , { time_it<T, Ind + 1 >(cppsort::selection_sort, Dist{})... } },
105+ { " low_comparisons_sorter" , { time_it<T, Ind + 1 >(low_comparisons_sorter{}, Dist{})... } },
106+ { " low_moves_sorter" , { time_it<T, Ind + 1 >(low_moves_sorter{}, Dist{})... } },
107+ { " merge_exchange_network_sorter" , { time_it<T, Ind + 1 >(merge_exchange_network_sorter{}, Dist{})... } },
108+ { " sorting_network_sorter" , { time_it<T, Ind + 1 >(sorting_network_sorter{}, Dist{})... } },
107109 };
108110
109111 // Output the results to their respective files
110112 std::ofstream output (Distribution::output);
111113 for (auto && sort_result: results) {
112- output << std::get<0 >(sort_result) << ' ' ;
114+ output << std::get<0 >(sort_result) << ' , ' ;
113115 for (auto && nb_cycles: std::get<1 >(sort_result)) {
114- output << nb_cycles << ' ' ;
116+ output << nb_cycles << ' , ' ;
115117 }
116118 output << ' \n ' ;
117119 }
@@ -125,7 +127,7 @@ template<
125127auto time_distributions ()
126128 -> void
127129{
128- using indices = std::make_index_sequence<N>;
130+ using indices = std::make_index_sequence<N - 1 >;
129131
130132 // Variadic dispatch only works with expressions
131133 int dummy[] = {
0 commit comments