From 02e1c566f7baf69c9ae33b9ec7a40c20fe054de9 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Mon, 5 Jun 2023 16:03:18 +0300 Subject: [PATCH] Copy boost::timer to tests since the original is deprecated and giving errors. We're not using Boost.Timer v2 components to avoid linking tests with its separately compiled binary. The legacy boost::timer is fine for the test purposes. --- test/CMakeLists.txt | 2 ++ test/Jamfile.v2 | 1 + test/efficiency.cpp | 8 +++--- test/timer.hpp | 59 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/timer.hpp diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3cdc7d0..56a8d0c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -5,6 +5,8 @@ # NOTE: CMake support for Boost.Parameter is currently experimental at best # and the interface is likely to change in the future +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) + # TODO: Also process literate tests file(GLOB test_files *.cpp) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index d36e184..d0fc78b 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -13,6 +13,7 @@ project boost/parameter : default-build off + . ; alias parameter_standard_tests diff --git a/test/efficiency.cpp b/test/efficiency.cpp index ddfb62a..4767d88 100644 --- a/test/efficiency.cpp +++ b/test/efficiency.cpp @@ -5,8 +5,8 @@ #include #include -#include #include +#include "timer.hpp" namespace test { @@ -147,7 +147,7 @@ namespace test { test::hammer(x, repeats); // Now start a timer. - boost::timer time; + test::timer time; test::hammer(x, repeats); // This time, we'll measure. return time.elapsed(); } @@ -163,7 +163,7 @@ int main() { repeats *= 10; - boost::timer time; + test::timer time; test::hammer >(.1, repeats); test::hammer >( @@ -172,7 +172,7 @@ int main() measured = time.elapsed(); } - + std::cout << "plain time: " << test::measure >( diff --git a/test/timer.hpp b/test/timer.hpp new file mode 100644 index 0000000..1f5823a --- /dev/null +++ b/test/timer.hpp @@ -0,0 +1,59 @@ +// Copyright Andrey Semashev 2023. +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// This file is a copy of boost/timer.hpp from Boost.Timer v1, which +// was deprecated and slated for removal. We are not using Boost.Timer v2 +// components to avoid having to link with its binary. +// +// See http://www.boost.org/libs/timer for documentation. + +#ifndef BOOST_PARAMETER_TEST_TIMER_HPP +#define BOOST_PARAMETER_TEST_TIMER_HPP + +#include +#include + +namespace test { + +// timer -------------------------------------------------------------------// + +// A timer object measures elapsed time. + +// It is recommended that implementations measure wall clock rather than CPU +// time since the intended use is performance measurement on systems where +// total elapsed time is more important than just process or CPU time. + +// Warnings: The maximum measurable elapsed time may well be only 596.5+ hours +// due to implementation limitations. The accuracy of timings depends on the +// accuracy of timing information provided by the underlying platform, and +// this varies a great deal from platform to platform. + +class timer +{ + public: + timer() { _start_time = std::clock(); } // postcondition: elapsed()==0 + void restart() { _start_time = std::clock(); } // post: elapsed()==0 + double elapsed() const // return elapsed time in seconds + { return double(std::clock() - _start_time) / CLOCKS_PER_SEC; } + + double elapsed_max() const // return estimated maximum value for elapsed() + // Portability warning: elapsed_max() may return too high a value on systems + // where std::clock_t overflows or resets at surprising values. + { + return (double((std::numeric_limits::max)()) + - double(_start_time)) / double(CLOCKS_PER_SEC); + } + + double elapsed_min() const // return minimum value for elapsed() + { return double(1)/double(CLOCKS_PER_SEC); } + + private: + std::clock_t _start_time; +}; // timer + +} // namespace test + +#endif // BOOST_PARAMETER_TEST_TIMER_HPP