Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple minimally working metrics (#214)
- Loading branch information
Showing
22 changed files
with
1,331 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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...)> | ||
{}; | ||
} |
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
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
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
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,82 @@ | ||
*New in version 1.15.0* | ||
|
||
*Metrics* are special kinds of [*sorter adapters*][sorter-adapters] that can be used to compute properties about a sorting algorithm, such as the number of comparisons performed while sorting a given collection, or the time it took to sort. As *sorter adapters*, metrics follow the *unified sorting interface*, and *do* sort the passed collection before returning the result. | ||
|
||
The anatomy of a metric adapter is thus close to that of any *sorter adapter* with the following additional properties: | ||
* The result of the *adapted sorter*'s is replaced with an instance of [`cppsort:::utility::metric`][utility-metrics-tools], which is a small wrapper type encapsulating a value. | ||
* It exposes a `tag_t` aliasing the tag of the returned metric value. | ||
* It exposes a `metric_t` type aliasing the returned metric type. | ||
|
||
The metrics tag can be any type, generally ending with the `_tag` suffix, and can be either empty or contain freeform static metadata about the kind of metric that uses it. Future versions of **cpp-sort** might standardize some tag fields. | ||
|
||
## Available metrics | ||
|
||
All available metrics live in the subnamespace `cppsort::metrics`. Even though all of them are available in their own standalone header, it is possible to include all of them at once with the following include: | ||
|
||
```cpp | ||
#include <cpp-sort/metrics.h> | ||
``` | ||
|
||
All of the metrics headers also includes `<cpp-sort/utility/metrics_tools.h>`. | ||
|
||
### `comparisons` | ||
|
||
```cpp | ||
#include <cpp-sort/metrics/comparisons.h> | ||
``` | ||
|
||
Computes the number of comparisons performed by the *adapted sorter*. | ||
|
||
This is done by wrapping the passed comparison function. As such it only works with sorters that accept a comparison, and bypasses components that special-case specific comparison objects. | ||
|
||
```cpp | ||
template< | ||
typename Sorter, | ||
typename CountType = std::size_t | ||
> | ||
struct comparisons; | ||
``` | ||
|
||
Returns an instance of `utility::metric<CountType, comparisons_tag>`. | ||
|
||
### `projections` | ||
|
||
```cpp | ||
#include <cpp-sort/metrics/projections.h> | ||
``` | ||
|
||
Computes the number of projections performed by the *adapted sorter*. | ||
|
||
This is done by wrapping the passed projection function. As such it only works with sorters that accept a projection, and bypasses components that special-case specific projection objects. | ||
|
||
```cpp | ||
template< | ||
typename Sorter, | ||
typename CountType = std::size_t | ||
> | ||
struct projections; | ||
``` | ||
|
||
Returns an instance of `utility::metric<CountType, projections_tag>`. | ||
|
||
### `running_time` | ||
|
||
```cpp | ||
#include <cpp-sort/metrics/running_time.h> | ||
``` | ||
|
||
Computes the time it takes for the *adapted sorter* to sort a collection, using a steady clock by default. | ||
|
||
```cpp | ||
template< | ||
typename Sorter, | ||
typename DurationType = typename std::chrono::steady_clock::duration | ||
> | ||
struct running_time; | ||
``` | ||
|
||
Returns an instance of `utility::metric<DurationType, running_type_tag>`. | ||
|
||
|
||
[sorter-adapters]: Sorter-adapters.md | ||
[utility-metrics-tools]: Miscellaneous-utilities.md#metrics-tools |
Oops, something went wrong.