Skip to content

Commit

Permalink
Add metrics::running_time
Browse files Browse the repository at this point in the history
  • Loading branch information
Morwenn committed Jul 1, 2023
1 parent 0415e2e commit f0a1214
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/cpp-sort/fwd.h
Expand Up @@ -8,6 +8,7 @@
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <chrono>
#include <cstddef>

namespace cppsort
Expand Down Expand Up @@ -109,6 +110,11 @@ namespace cppsort
struct comparisons;
template<typename Sorter, typename CountType=std::size_t>
struct projections;
template<
typename Sorter,
typename DurationType = typename std::chrono::steady_clock::duration
>
struct running_time;
}
}

Expand Down
72 changes: 72 additions & 0 deletions include/cpp-sort/metrics/running_time.h
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2023 Morwenn
* SPDX-License-Identifier: MIT
*/
#ifndef CPPSORT_METRICS_RUNNING_TIME_H_
#define CPPSORT_METRICS_RUNNING_TIME_H_

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <chrono>
#include <utility>
#include <cpp-sort/fwd.h>
#include <cpp-sort/utility/adapter_storage.h>
#include <cpp-sort/utility/metrics_tools.h>
#include "../detail/checkers.h"
#include "../detail/type_traits.h"

namespace cppsort
{
namespace metrics
{
////////////////////////////////////////////////////////////
// Tag

struct running_time_tag {};

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

template<typename Sorter, typename DurationType>
struct running_time:
utility::adapter_storage<Sorter>,
cppsort::detail::check_iterator_category<Sorter>,
cppsort::detail::check_is_always_stable<Sorter>
{
using tag_t = running_time_tag;
using metric_t = utility::metric<DurationType, tag_t>;

running_time() = default;

constexpr explicit running_time(Sorter sorter):
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<DurationType>())
)
{
auto start = std::chrono::steady_clock::now();
this->get()(std::forward<Args>(args)...);
auto stop = std::chrono::steady_clock::now();
return metric_t(std::chrono::duration_cast<DurationType>(stop - start));
}
};
}}

namespace cppsort
{
////////////////////////////////////////////////////////////
// is_stable specialization

template<typename Sorter, typename DurationType, typename... Args>
struct is_stable<metrics::running_time<Sorter, DurationType>(Args...)>:
is_stable<Sorter(Args...)>
{};
}

#endif // CPPSORT_METRICS_RUNNING_TIME_H_
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Expand Up @@ -155,6 +155,7 @@ add_executable(main-tests
# Metrics tests
metrics/comparisons.cpp
metrics/projections.cpp
metrics/running_time.cpp

# Comparators tests
comparators/case_insensitive_less.cpp
Expand Down
46 changes: 46 additions & 0 deletions tests/metrics/running_time.cpp
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 Morwenn
* SPDX-License-Identifier: MIT
*/
#include <algorithm>
#include <chrono>
#include <iterator>
#include <list>
#include <catch2/catch_test_macros.hpp>
#include <cpp-sort/metrics/running_time.h>
#include <cpp-sort/sorters/splay_sorter.h>
#include <testing-tools/distributions.h>

TEST_CASE( "basic metrics::running_time tests", "[metrics]" )
{
using namespace std::chrono_literals;

std::list<int> collection;
auto distribution = dist::shuffled{};
distribution(std::back_inserter(collection), 102);

SECTION( "with default duration type" )
{
cppsort::metrics::running_time<
cppsort::splay_sorter
> sorter;

auto res = sorter(collection);
CHECK( res > 0s );
CHECK( std::is_sorted(collection.begin(), collection.end()) );
}

SECTION( "with explicit duration type" )
{
cppsort::metrics::running_time<
cppsort::splay_sorter,
std::chrono::milliseconds
> sorter;

auto res = sorter(collection);
CHECK( res >= 0s );
CHECK( res <= 1s );
CHECK( std::is_sorted(collection.begin(), collection.end()) );
}

}

0 comments on commit f0a1214

Please sign in to comment.