Skip to content

Commit

Permalink
benchmarks: add cpu_cycles metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Jul 2, 2023
1 parent 0dfc4c5 commit f9c19cb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
58 changes: 58 additions & 0 deletions benchmarks/benchmarking-tools/cpu_cycles.h
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 Morwenn
* SPDX-License-Identifier: MIT
*/
#include <cstdint>
#include <utility>
#include <cpp-sort/utility/adapter_storage.h>
#include <cpp-sort/utility/metrics_tools.h>
#include <cpp-sort/detail/checkers.h>
#include "rdtsc.h"

////////////////////////////////////////////////////////////
// Tag

struct cpu_cycles_tag {};

////////////////////////////////////////////////////////////
// Metric

template<typename Sorter>
struct cpu_cycles:
cppsort::utility::adapter_storage<Sorter>,
cppsort::detail::check_iterator_category<Sorter>,
cppsort::detail::check_is_always_stable<Sorter>
{
using tag_t = cpu_cycles_tag;
using metric_t = cppsort::utility::metric<unsigned long long, tag_t>;

cpu_cycles() = default;

constexpr explicit cpu_cycles(Sorter sorter):
cppsort::utility::adapter_storage<Sorter>(std::move(sorter))
{}

template<typename... Args>
auto operator()(Args&&... args) const
-> decltype(
this->get()(std::forward<Args>(args)...),
metric_t(std::declval<unsigned long long>())
)
{
auto start = ::rdtsc();
this->get()(std::forward<Args>(args)...);
auto stop = ::rdtsc();
return metric_t(stop - start);
}
};

////////////////////////////////////////////////////////////
// is_stable specialization

namespace cppsort
{
template<typename Sorter, typename... Args>
struct is_stable<cpu_cycles<Sorter>(Args...)>:
is_stable<Sorter(Args...)>
{};
}
4 changes: 3 additions & 1 deletion benchmarks/benchmarking-tools/rdtsc.h
Expand Up @@ -26,7 +26,9 @@

#ifdef _WIN32
#include <intrin.h>
#define rdtsc __rdtsc
inline unsigned long long rdtsc() {
return __rdtsc();
}
#else
#ifdef __i586__
static __inline__ unsigned long long rdtsc() {
Expand Down
1 change: 0 additions & 1 deletion benchmarks/patterns/bars.py
Expand Up @@ -63,7 +63,6 @@ def main():
"quick_sort",
"ska_sort",
"std_sort",
"verge_sort",
]

root = pathlib.Path(args.root)
Expand Down
18 changes: 8 additions & 10 deletions benchmarks/patterns/bench.cpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015-2021 Morwenn
* Copyright (c) 2015-2023 Morwenn
* SPDX-License-Identifier: MIT
*/

Expand Down Expand Up @@ -36,8 +36,8 @@
#include <utility>
#include <vector>
#include <cpp-sort/sorters.h>
#include "../benchmarking-tools/cpu_cycles.h"
#include "../benchmarking-tools/distributions.h"
#include "../benchmarking-tools/rdtsc.h"

// Type of data to sort during the benchmark
using value_t = double;
Expand Down Expand Up @@ -78,7 +78,6 @@ int main()
{ "quick_sort", cppsort::quick_sort },
{ "ska_sort", cppsort::ska_sort },
{ "std_sort", cppsort::std_sort },
{ "verge_sort", cppsort::verge_sort },
};

std::size_t sizes[] = { 1'000'000 };
Expand All @@ -94,26 +93,25 @@ int main()
distributions_prng.seed(seed);

for (auto size: sizes) {
std::vector<std::uint64_t> cycles;
std::vector<std::uint64_t> cycles_per_element;

auto total_start = clock_type::now();
auto total_end = clock_type::now();
while (total_end - total_start < 5s) {
collection_t collection;
distribution.second(std::back_inserter(collection), size);
std::uint64_t start = rdtsc();
sort.second(collection);
std::uint64_t end = rdtsc();
auto do_sort = cpu_cycles<sort_f>(sort.second);
auto nb_cycles = do_sort(collection);
assert(std::is_sorted(std::begin(collection), std::end(collection)));
cycles.push_back(double(end - start) / size + 0.5);
cycles_per_element.push_back(double(nb_cycles.value()) / size + 0.5);
total_end = clock_type::now();
}

for (std::ostream* stream: {&std::cout, &std::cerr}) {
(*stream) << size << ", " << distribution.first << ", " << sort.first << ", ";
auto it = cycles.begin();
auto it = cycles_per_element.begin();
(*stream) << *it;
while (++it != cycles.end()) {
while (++it != cycles_per_element.end()) {
(*stream) << ", " << *it;
}
(*stream) << std::endl;
Expand Down

0 comments on commit f9c19cb

Please sign in to comment.