Warning, file /include/catch2/benchmark/catch_chronometer.hpp was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef CATCH_CHRONOMETER_HPP_INCLUDED
0011 #define CATCH_CHRONOMETER_HPP_INCLUDED
0012
0013 #include <catch2/benchmark/catch_clock.hpp>
0014 #include <catch2/benchmark/catch_optimizer.hpp>
0015 #include <catch2/internal/catch_meta.hpp>
0016 #include <catch2/internal/catch_move_and_forward.hpp>
0017
0018 namespace Catch {
0019 namespace Benchmark {
0020 namespace Detail {
0021 struct ChronometerConcept {
0022 virtual void start() = 0;
0023 virtual void finish() = 0;
0024 virtual ~ChronometerConcept();
0025
0026 ChronometerConcept() = default;
0027 ChronometerConcept(ChronometerConcept const&) = default;
0028 ChronometerConcept& operator=(ChronometerConcept const&) = default;
0029 };
0030 template <typename Clock>
0031 struct ChronometerModel final : public ChronometerConcept {
0032 void start() override { started = Clock::now(); }
0033 void finish() override { finished = Clock::now(); }
0034
0035 IDuration elapsed() const {
0036 return std::chrono::duration_cast<std::chrono::nanoseconds>(
0037 finished - started );
0038 }
0039
0040 TimePoint<Clock> started;
0041 TimePoint<Clock> finished;
0042 };
0043 }
0044
0045 struct Chronometer {
0046 public:
0047 template <typename Fun>
0048 void measure(Fun&& fun) { measure(CATCH_FORWARD(fun), is_callable<Fun(int)>()); }
0049
0050 int runs() const { return repeats; }
0051
0052 Chronometer(Detail::ChronometerConcept& meter, int repeats_)
0053 : impl(&meter)
0054 , repeats(repeats_) {}
0055
0056 private:
0057 template <typename Fun>
0058 void measure(Fun&& fun, std::false_type) {
0059 measure([&fun](int) { return fun(); }, std::true_type());
0060 }
0061
0062 template <typename Fun>
0063 void measure(Fun&& fun, std::true_type) {
0064 Detail::optimizer_barrier();
0065 impl->start();
0066 for (int i = 0; i < repeats; ++i) invoke_deoptimized(fun, i);
0067 impl->finish();
0068 Detail::optimizer_barrier();
0069 }
0070
0071 Detail::ChronometerConcept* impl;
0072 int repeats;
0073 };
0074 }
0075 }
0076
0077 #endif