Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) ); | ||
} | ||
|
||
} |