Skip to content

Commit

Permalink
Replace vector iterator args in benchmarks with ptr args
Browse files Browse the repository at this point in the history
  • Loading branch information
horenmar committed Sep 8, 2023
1 parent b4ffba5 commit 9bba07c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 48 deletions.
9 changes: 7 additions & 2 deletions src/catch2/benchmark/detail/catch_analyse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ namespace Catch {
samples.push_back( current->count() );
}

auto analysis = Catch::Benchmark::Detail::analyse_samples(cfg.benchmarkConfidenceInterval(), cfg.benchmarkResamples(), samples.begin(), samples.end());
auto outliers = Catch::Benchmark::Detail::classify_outliers(samples.begin(), samples.end());
auto analysis = Catch::Benchmark::Detail::analyse_samples(
cfg.benchmarkConfidenceInterval(),
cfg.benchmarkResamples(),
samples.data(),
samples.data() + samples.size() );
auto outliers = Catch::Benchmark::Detail::classify_outliers(
samples.data(), samples.data() + samples.size() );

auto wrap_estimate = [](Estimate<double> e) {
return Estimate<Duration> {
Expand Down
8 changes: 4 additions & 4 deletions src/catch2/benchmark/detail/catch_estimate_clock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ namespace Catch {
auto r = run_for_at_least<Clock>(std::chrono::duration_cast<ClockDuration<Clock>>(clock_resolution_estimation_time), iterations, &resolution<Clock>)
.result;
return {
FloatDuration<Clock>(mean(r.begin(), r.end())),
classify_outliers(r.begin(), r.end()),
FloatDuration<Clock>(mean(r.data(), r.data() + r.size())),
classify_outliers(r.data(), r.data() + r.size()),
};
}
template <typename Clock>
Expand Down Expand Up @@ -92,8 +92,8 @@ namespace Catch {
.count() ) );
}
return {
FloatDuration<Clock>(mean(times.begin(), times.end())),
classify_outliers(times.begin(), times.end()),
FloatDuration<Clock>(mean(times.data(), times.data() + times.size())),
classify_outliers(times.data(), times.data() + times.size()),
};
}

Expand Down
34 changes: 16 additions & 18 deletions src/catch2/benchmark/detail/catch_stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace Catch {
static sample
resample( URng& rng,
unsigned int resamples,
std::vector<double>::const_iterator first,
std::vector<double>::const_iterator last,
double const* first,
double const* last,
Estimator& estimator ) {
auto n = static_cast<size_t>( last - first );
std::uniform_int_distribution<decltype( n )> dist( 0,
Expand All @@ -51,7 +51,7 @@ namespace Catch {
dist( rng ) )] );
}
const auto estimate =
estimator( resampled.begin(), resampled.end() );
estimator( resampled.data(), resampled.data() + resampled.size() );
out.push_back( estimate );
}
std::sort( out.begin(), out.end() );
Expand Down Expand Up @@ -168,8 +168,7 @@ namespace Catch {
}

static double
standard_deviation( std::vector<double>::const_iterator first,
std::vector<double>::const_iterator last ) {
standard_deviation( double const* first, double const* last ) {
auto m = Catch::Benchmark::Detail::mean( first, last );
double variance =
std::accumulate( first,
Expand Down Expand Up @@ -201,7 +200,10 @@ namespace Catch {
# pragma GCC diagnostic pop
#endif

double weighted_average_quantile(int k, int q, std::vector<double>::iterator first, std::vector<double>::iterator last) {
double weighted_average_quantile( int k,
int q,
double* first,
double* last ) {
auto count = last - first;
double idx = (count - 1) * k / static_cast<double>(q);
int j = static_cast<int>(idx);
Expand All @@ -217,12 +219,11 @@ namespace Catch {
}

OutlierClassification
classify_outliers( std::vector<double>::const_iterator first,
std::vector<double>::const_iterator last ) {
classify_outliers( double const* first, double const* last ) {
std::vector<double> copy( first, last );

auto q1 = weighted_average_quantile( 1, 4, copy.begin(), copy.end() );
auto q3 = weighted_average_quantile( 3, 4, copy.begin(), copy.end() );
auto q1 = weighted_average_quantile( 1, 4, copy.data(), copy.data() + copy.size() );
auto q3 = weighted_average_quantile( 3, 4, copy.data(), copy.data() + copy.size() );
auto iqr = q3 - q1;
auto los = q1 - ( iqr * 3. );
auto lom = q1 - ( iqr * 1.5 );
Expand All @@ -246,8 +247,7 @@ namespace Catch {
return o;
}

double mean( std::vector<double>::const_iterator first,
std::vector<double>::const_iterator last ) {
double mean( double const* first, double const* last ) {
auto count = last - first;
double sum = 0.;
while (first != last) {
Expand Down Expand Up @@ -280,8 +280,8 @@ namespace Catch {

bootstrap_analysis analyse_samples(double confidence_level,
unsigned int n_resamples,
std::vector<double>::iterator first,
std::vector<double>::iterator last) {
double* first,
double* last) {
CATCH_INTERNAL_START_WARNINGS_SUPPRESSION
CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS
static std::random_device entropy;
Expand All @@ -293,8 +293,7 @@ namespace Catch {
auto stddev = &standard_deviation;

#if defined(CATCH_CONFIG_USE_ASYNC)
auto Estimate = [=](double(*f)(std::vector<double>::const_iterator,
std::vector<double>::const_iterator)) {
auto Estimate = [=](double(*f)(double const*, double const*)) {
auto seed = entropy();
return std::async(std::launch::async, [=] {
std::mt19937 rng(seed);
Expand All @@ -309,8 +308,7 @@ namespace Catch {
auto mean_estimate = mean_future.get();
auto stddev_estimate = stddev_future.get();
#else
auto Estimate = [=](double(*f)(std::vector<double>::const_iterator,
std::vector<double>::const_iterator)) {
auto Estimate = [=](double(*f)(double const* , double const*)) {
auto seed = entropy();
std::mt19937 rng(seed);
auto resampled = resample(rng, n_resamples, first, last, f);
Expand Down
25 changes: 13 additions & 12 deletions src/catch2/benchmark/detail/catch_stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,20 @@ namespace Catch {
// to centralize warning suppression
bool directCompare( double lhs, double rhs );

double weighted_average_quantile(int k, int q, std::vector<double>::iterator first, std::vector<double>::iterator last);
double weighted_average_quantile( int k,
int q,
double* first,
double* last );

OutlierClassification
classify_outliers( std::vector<double>::const_iterator first,
std::vector<double>::const_iterator last );
classify_outliers( double const* first, double const* last );

double mean( std::vector<double>::const_iterator first,
std::vector<double>::const_iterator last );
double mean( double const* first, double const* last );

template <typename Estimator>
sample jackknife(Estimator&& estimator,
std::vector<double>::iterator first,
std::vector<double>::iterator last) {
double* first,
double* last) {
auto n = static_cast<size_t>(last - first);
auto second = first;
++second;
Expand All @@ -63,8 +64,8 @@ namespace Catch {

template <typename Estimator>
Estimate<double> bootstrap( double confidence_level,
std::vector<double>::iterator first,
std::vector<double>::iterator last,
double* first,
double* last,
sample const& resample,
Estimator&& estimator ) {
auto n_samples = last - first;
Expand All @@ -74,7 +75,7 @@ namespace Catch {
if (n_samples == 1) return { point, point, point, confidence_level };

sample jack = jackknife(estimator, first, last);
double jack_mean = mean(jack.begin(), jack.end());
double jack_mean = mean(jack.data(), jack.data() + jack.size());
double sum_squares = 0, sum_cubes = 0;
for (double x : jack) {
auto difference = jack_mean - x;
Expand Down Expand Up @@ -116,8 +117,8 @@ namespace Catch {

bootstrap_analysis analyse_samples(double confidence_level,
unsigned int n_resamples,
std::vector<double>::iterator first,
std::vector<double>::iterator last);
double* first,
double* last);
} // namespace Detail
} // namespace Benchmark
} // namespace Catch
Expand Down
34 changes: 22 additions & 12 deletions tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,12 @@ TEST_CASE("uniform samples", "[benchmark]") {
std::vector<double> samples(100);
std::fill(samples.begin(), samples.end(), 23);

using it = std::vector<double>::iterator;
auto e = Catch::Benchmark::Detail::bootstrap(0.95, samples.begin(), samples.end(), samples, [](it a, it b) {
auto e = Catch::Benchmark::Detail::bootstrap(
0.95,
samples.data(),
samples.data() + samples.size(),
samples,
[]( double const* a, double const* b ) {
auto sum = std::accumulate(a, b, 0.);
return sum / (b - a);
});
Expand Down Expand Up @@ -198,17 +202,17 @@ TEST_CASE("normal_quantile", "[benchmark]") {
TEST_CASE("mean", "[benchmark]") {
std::vector<double> x{ 10., 20., 14., 16., 30., 24. };

auto m = Catch::Benchmark::Detail::mean(x.begin(), x.end());
auto m = Catch::Benchmark::Detail::mean(x.data(), x.data() + x.size());

REQUIRE(m == 19.);
}

TEST_CASE("weighted_average_quantile", "[benchmark]") {
std::vector<double> x{ 10., 20., 14., 16., 30., 24. };

auto q1 = Catch::Benchmark::Detail::weighted_average_quantile(1, 4, x.begin(), x.end());
auto med = Catch::Benchmark::Detail::weighted_average_quantile(1, 2, x.begin(), x.end());
auto q3 = Catch::Benchmark::Detail::weighted_average_quantile(3, 4, x.begin(), x.end());
auto q1 = Catch::Benchmark::Detail::weighted_average_quantile(1, 4, x.data(), x.data() + x.size());
auto med = Catch::Benchmark::Detail::weighted_average_quantile(1, 2, x.data(), x.data() + x.size());
auto q3 = Catch::Benchmark::Detail::weighted_average_quantile(3, 4, x.data(), x.data() + x.size());

REQUIRE(q1 == 14.5);
REQUIRE(med == 18.);
Expand All @@ -227,47 +231,53 @@ TEST_CASE("classify_outliers", "[benchmark]") {
SECTION("none") {
std::vector<double> x{ 10., 20., 14., 16., 30., 24. };

auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
auto o = Catch::Benchmark::Detail::classify_outliers(
x.data(), x.data() + x.size() );

REQUIRE(o.samples_seen == static_cast<int>(x.size()));
require_outliers(o, 0, 0, 0, 0);
}
SECTION("low severe") {
std::vector<double> x{ -12., 20., 14., 16., 30., 24. };

auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
auto o = Catch::Benchmark::Detail::classify_outliers(
x.data(), x.data() + x.size() );

REQUIRE(o.samples_seen == static_cast<int>(x.size()));
require_outliers(o, 1, 0, 0, 0);
}
SECTION("low mild") {
std::vector<double> x{ 1., 20., 14., 16., 30., 24. };

auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
auto o = Catch::Benchmark::Detail::classify_outliers(
x.data(), x.data() + x.size() );

REQUIRE(o.samples_seen == static_cast<int>(x.size()));
require_outliers(o, 0, 1, 0, 0);
}
SECTION("high mild") {
std::vector<double> x{ 10., 20., 14., 16., 36., 24. };

auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
auto o = Catch::Benchmark::Detail::classify_outliers(
x.data(), x.data() + x.size() );

REQUIRE(o.samples_seen == static_cast<int>(x.size()));
require_outliers(o, 0, 0, 1, 0);
}
SECTION("high severe") {
std::vector<double> x{ 10., 20., 14., 16., 49., 24. };

auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
auto o = Catch::Benchmark::Detail::classify_outliers(
x.data(), x.data() + x.size() );

REQUIRE(o.samples_seen == static_cast<int>(x.size()));
require_outliers(o, 0, 0, 0, 1);
}
SECTION("mixed") {
std::vector<double> x{ -20., 20., 14., 16., 39., 24. };

auto o = Catch::Benchmark::Detail::classify_outliers(x.begin(), x.end());
auto o = Catch::Benchmark::Detail::classify_outliers(
x.data(), x.data() + x.size() );

REQUIRE(o.samples_seen == static_cast<int>(x.size()));
require_outliers(o, 1, 0, 1, 0);
Expand Down

0 comments on commit 9bba07c

Please sign in to comment.