diff --git a/src/catch2/benchmark/detail/catch_analyse.hpp b/src/catch2/benchmark/detail/catch_analyse.hpp index c932ff26..60db7d87 100644 --- a/src/catch2/benchmark/detail/catch_analyse.hpp +++ b/src/catch2/benchmark/detail/catch_analyse.hpp @@ -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 e) { return Estimate { diff --git a/src/catch2/benchmark/detail/catch_estimate_clock.hpp b/src/catch2/benchmark/detail/catch_estimate_clock.hpp index 1e916ae4..a0dc2d3d 100644 --- a/src/catch2/benchmark/detail/catch_estimate_clock.hpp +++ b/src/catch2/benchmark/detail/catch_estimate_clock.hpp @@ -63,8 +63,8 @@ namespace Catch { auto r = run_for_at_least(std::chrono::duration_cast>(clock_resolution_estimation_time), iterations, &resolution) .result; return { - FloatDuration(mean(r.begin(), r.end())), - classify_outliers(r.begin(), r.end()), + FloatDuration(mean(r.data(), r.data() + r.size())), + classify_outliers(r.data(), r.data() + r.size()), }; } template @@ -92,8 +92,8 @@ namespace Catch { .count() ) ); } return { - FloatDuration(mean(times.begin(), times.end())), - classify_outliers(times.begin(), times.end()), + FloatDuration(mean(times.data(), times.data() + times.size())), + classify_outliers(times.data(), times.data() + times.size()), }; } diff --git a/src/catch2/benchmark/detail/catch_stats.cpp b/src/catch2/benchmark/detail/catch_stats.cpp index ea483a30..caad0167 100644 --- a/src/catch2/benchmark/detail/catch_stats.cpp +++ b/src/catch2/benchmark/detail/catch_stats.cpp @@ -30,8 +30,8 @@ namespace Catch { static sample resample( URng& rng, unsigned int resamples, - std::vector::const_iterator first, - std::vector::const_iterator last, + double const* first, + double const* last, Estimator& estimator ) { auto n = static_cast( last - first ); std::uniform_int_distribution dist( 0, @@ -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() ); @@ -168,8 +168,7 @@ namespace Catch { } static double - standard_deviation( std::vector::const_iterator first, - std::vector::const_iterator last ) { + standard_deviation( double const* first, double const* last ) { auto m = Catch::Benchmark::Detail::mean( first, last ); double variance = std::accumulate( first, @@ -201,7 +200,10 @@ namespace Catch { # pragma GCC diagnostic pop #endif - double weighted_average_quantile(int k, int q, std::vector::iterator first, std::vector::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(q); int j = static_cast(idx); @@ -217,12 +219,11 @@ namespace Catch { } OutlierClassification - classify_outliers( std::vector::const_iterator first, - std::vector::const_iterator last ) { + classify_outliers( double const* first, double const* last ) { std::vector 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 ); @@ -246,8 +247,7 @@ namespace Catch { return o; } - double mean( std::vector::const_iterator first, - std::vector::const_iterator last ) { + double mean( double const* first, double const* last ) { auto count = last - first; double sum = 0.; while (first != last) { @@ -280,8 +280,8 @@ namespace Catch { bootstrap_analysis analyse_samples(double confidence_level, unsigned int n_resamples, - std::vector::iterator first, - std::vector::iterator last) { + double* first, + double* last) { CATCH_INTERNAL_START_WARNINGS_SUPPRESSION CATCH_INTERNAL_SUPPRESS_GLOBALS_WARNINGS static std::random_device entropy; @@ -293,8 +293,7 @@ namespace Catch { auto stddev = &standard_deviation; #if defined(CATCH_CONFIG_USE_ASYNC) - auto Estimate = [=](double(*f)(std::vector::const_iterator, - std::vector::const_iterator)) { + auto Estimate = [=](double(*f)(double const*, double const*)) { auto seed = entropy(); return std::async(std::launch::async, [=] { std::mt19937 rng(seed); @@ -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::const_iterator, - std::vector::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); diff --git a/src/catch2/benchmark/detail/catch_stats.hpp b/src/catch2/benchmark/detail/catch_stats.hpp index c1ce5664..54621670 100644 --- a/src/catch2/benchmark/detail/catch_stats.hpp +++ b/src/catch2/benchmark/detail/catch_stats.hpp @@ -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::iterator first, std::vector::iterator last); + double weighted_average_quantile( int k, + int q, + double* first, + double* last ); OutlierClassification - classify_outliers( std::vector::const_iterator first, - std::vector::const_iterator last ); + classify_outliers( double const* first, double const* last ); - double mean( std::vector::const_iterator first, - std::vector::const_iterator last ); + double mean( double const* first, double const* last ); template sample jackknife(Estimator&& estimator, - std::vector::iterator first, - std::vector::iterator last) { + double* first, + double* last) { auto n = static_cast(last - first); auto second = first; ++second; @@ -63,8 +64,8 @@ namespace Catch { template Estimate bootstrap( double confidence_level, - std::vector::iterator first, - std::vector::iterator last, + double* first, + double* last, sample const& resample, Estimator&& estimator ) { auto n_samples = last - first; @@ -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; @@ -116,8 +117,8 @@ namespace Catch { bootstrap_analysis analyse_samples(double confidence_level, unsigned int n_resamples, - std::vector::iterator first, - std::vector::iterator last); + double* first, + double* last); } // namespace Detail } // namespace Benchmark } // namespace Catch diff --git a/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp b/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp index 24bfe68c..34ed8814 100644 --- a/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp +++ b/tests/SelfTest/IntrospectiveTests/InternalBenchmark.tests.cpp @@ -156,8 +156,12 @@ TEST_CASE("uniform samples", "[benchmark]") { std::vector samples(100); std::fill(samples.begin(), samples.end(), 23); - using it = std::vector::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); }); @@ -198,7 +202,7 @@ TEST_CASE("normal_quantile", "[benchmark]") { TEST_CASE("mean", "[benchmark]") { std::vector 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.); } @@ -206,9 +210,9 @@ TEST_CASE("mean", "[benchmark]") { TEST_CASE("weighted_average_quantile", "[benchmark]") { std::vector 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.); @@ -227,7 +231,8 @@ TEST_CASE("classify_outliers", "[benchmark]") { SECTION("none") { std::vector 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(x.size())); require_outliers(o, 0, 0, 0, 0); @@ -235,7 +240,8 @@ TEST_CASE("classify_outliers", "[benchmark]") { SECTION("low severe") { std::vector 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(x.size())); require_outliers(o, 1, 0, 0, 0); @@ -243,7 +249,8 @@ TEST_CASE("classify_outliers", "[benchmark]") { SECTION("low mild") { std::vector 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(x.size())); require_outliers(o, 0, 1, 0, 0); @@ -251,7 +258,8 @@ TEST_CASE("classify_outliers", "[benchmark]") { SECTION("high mild") { std::vector 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(x.size())); require_outliers(o, 0, 0, 1, 0); @@ -259,7 +267,8 @@ TEST_CASE("classify_outliers", "[benchmark]") { SECTION("high severe") { std::vector 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(x.size())); require_outliers(o, 0, 0, 0, 1); @@ -267,7 +276,8 @@ TEST_CASE("classify_outliers", "[benchmark]") { SECTION("mixed") { std::vector 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(x.size())); require_outliers(o, 1, 0, 1, 0);