From 4fc4f36168d208fa82b46435ba6e8c43bd272aef Mon Sep 17 00:00:00 2001 From: boxanm Date: Sun, 12 Jan 2025 13:28:19 -0500 Subject: [PATCH 1/6] Replace boost::timer with std::chrono::steady_clock --- pointmatcher/Timer.cpp | 37 +++++++++++++++++++++++++++++++------ pointmatcher/Timer.h | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 13 deletions(-) diff --git a/pointmatcher/Timer.cpp b/pointmatcher/Timer.cpp index b7fee491..bd9f30e4 100644 --- a/pointmatcher/Timer.cpp +++ b/pointmatcher/Timer.cpp @@ -40,24 +40,25 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #endif -#ifdef _POSIX_TIMERS +#if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) namespace PointMatcherSupport { timer::timer(): _start_time(curTime()) { - } - + std::cout << "Using POSIX timer\n"; + } + void timer::restart() { _start_time = curTime(); } - + double timer::elapsed() const { return double(curTime() - _start_time) / double(1000000000); } - + timer::Time timer::curTime() const { #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time @@ -77,5 +78,29 @@ namespace PointMatcherSupport #endif // __MACH__ } } // namespace PointMatcherSupport -#endif // _POSIX_TIMERS +#else // _POSIX_TIMERS +namespace PointMatcherSupport +{ + //! Create and start the timer + timer::timer(): + _start_time(curTime()) + { + std::cout << "Using std::chrono timer\n"; + } + //! Restart the counter + void timer::restart() + { + _start_time = curTime(); + } + //! Return elapsed time in seconds + double timer::elapsed() const + { + return double(curTime() - _start_time) / double(1000000000); + } + timer::Time timer::curTime() const + { + return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); + } +}// namespace PointMatcherSupport +#endif // _POSIX_TIMERS diff --git a/pointmatcher/Timer.h b/pointmatcher/Timer.h index 316dbe28..cda4a76b 100644 --- a/pointmatcher/Timer.h +++ b/pointmatcher/Timer.h @@ -36,21 +36,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef __POINTMATCHER_TIMER_H #define __POINTMATCHER_TIMER_H +#include #include #ifndef WIN32 #include #endif // WIN32 -#ifdef _POSIX_TIMERS +#if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) namespace PointMatcherSupport { /** High-precision timer class, using clock_gettime() or clock_get_time() - + The interface is a subset of the one boost::timer provides, but the implementation is much more precise on systems where clock() has low precision, such as glibc. - + This code gets compiled if _POSIX_TIMERS is set, generally in time.h or unistd.h */ @@ -58,7 +59,7 @@ namespace PointMatcherSupport { //! 64-bit time typedef unsigned long long Time; - + //! Create and start the timer timer(); //! Restart the counter @@ -69,15 +70,37 @@ namespace PointMatcherSupport private: //! Return time at call Time curTime() const; - + Time _start_time; //! time when counter started }; } // namespace PointMatcherSupport #else // _POSIX_TIMERS -#include +#include namespace PointMatcherSupport { - typedef boost::timer timer; + + /** + This is an interface between std::chrono::steady_clock and lpm + This code gets compiled if _POSIX_TIMERS is not set + */ + struct timer + { + //! 64-bit time + typedef unsigned long long Time; + + //! Create and start the timer + timer(); + //! Restart the counter + void restart(); + //! Return elapsed time in seconds + double elapsed() const; + + private: + //! Return time at call + Time curTime() const; + + Time _start_time; //! time when counter started + }; } #endif // _POSIX_TIMERS From 8edbc4cd5e2b0e046d8cfe8c898a456e76949a34 Mon Sep 17 00:00:00 2001 From: boxanm Date: Sun, 12 Jan 2025 13:49:53 -0500 Subject: [PATCH 2/6] Simplify the Timer class --- pointmatcher/Timer.cpp | 74 +++++++++++++++++++----------------------- pointmatcher/Timer.h | 44 +++++-------------------- 2 files changed, 42 insertions(+), 76 deletions(-) diff --git a/pointmatcher/Timer.cpp b/pointmatcher/Timer.cpp index bd9f30e4..1ec1345a 100644 --- a/pointmatcher/Timer.cpp +++ b/pointmatcher/Timer.cpp @@ -40,13 +40,16 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #endif -#if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) namespace PointMatcherSupport { timer::timer(): _start_time(curTime()) { + #if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) std::cout << "Using POSIX timer\n"; + #else + std::cout << "Using std::chrono timer\n"; + #endif } void timer::restart() @@ -61,46 +64,35 @@ namespace PointMatcherSupport timer::Time timer::curTime() const { - #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time - clock_serv_t host_clock; - mach_timespec_t now; - host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &host_clock); - clock_get_time(host_clock, &now); - return Time(now.tv_sec) * Time(1000000000) + Time(now.tv_nsec); - #else // __MACH__ - struct timespec ts; - #ifdef CLOCK_PROCESS_CPUTIME_ID - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); - #else // BSD and old Linux - clock_gettime(CLOCK_PROF, &ts); - #endif - return Time(ts.tv_sec) * Time(1000000000) + Time(ts.tv_nsec); - #endif // __MACH__ + #if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) + #ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time + clock_serv_t host_clock; + mach_timespec_t now; + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &host_clock); + clock_get_time(host_clock, &now); + return Time(now.tv_sec) * Time(1000000000) + Time(now.tv_nsec); + #else // __MACH__ + struct timespec ts; + #ifdef CLOCK_PROCESS_CPUTIME_ID + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); + #else // BSD and old Linux + clock_gettime(CLOCK_PROF, &ts); + #endif + return Time(ts.tv_sec) * Time(1000000000) + Time(ts.tv_nsec); + #endif // __MACH__ + #else + return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); + #endif // _POSIX_TIMERS + } } // namespace PointMatcherSupport -#else // _POSIX_TIMERS -namespace PointMatcherSupport -{ - //! Create and start the timer - timer::timer(): - _start_time(curTime()) - { - std::cout << "Using std::chrono timer\n"; - } - //! Restart the counter - void timer::restart() - { - _start_time = curTime(); - } - //! Return elapsed time in seconds - double timer::elapsed() const - { - return double(curTime() - _start_time) / double(1000000000); - } +// #else // _POSIX_TIMERS +// namespace PointMatcherSupport +// { - timer::Time timer::curTime() const - { - return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); - } -}// namespace PointMatcherSupport -#endif // _POSIX_TIMERS +// timer::Time timer::curTime() const +// { +// return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); +// } +// }// namespace PointMatcherSupport +// #endif // _POSIX_TIMERS diff --git a/pointmatcher/Timer.h b/pointmatcher/Timer.h index cda4a76b..7b6d17e7 100644 --- a/pointmatcher/Timer.h +++ b/pointmatcher/Timer.h @@ -41,19 +41,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef WIN32 #include #endif // WIN32 +#if !defined(_POSIX_TIMERS) || defined(FORCE_DISABLE_POSIX_TIMERS) +#include +#endif -#if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) +// #if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) namespace PointMatcherSupport { /** - High-precision timer class, using clock_gettime() or clock_get_time() + This is an interface between std::chrono::steady_clock and lpm - The interface is a subset of the one boost::timer provides, - but the implementation is much more precise + Uses either std::chrono::steady_clock if _POSIX_TIMERS is not set, or + clock_gettime() or clock_get_time() if _POSIX_TIMERS is set, + generally in time.h or unistd.h. Then, the implementation is much more precise on systems where clock() has low precision, such as glibc. - This code gets compiled if _POSIX_TIMERS is set, - generally in time.h or unistd.h + The interface is a subset of the original boost::timer class. */ struct timer { @@ -74,34 +77,5 @@ namespace PointMatcherSupport Time _start_time; //! time when counter started }; } // namespace PointMatcherSupport -#else // _POSIX_TIMERS -#include -namespace PointMatcherSupport -{ - - /** - This is an interface between std::chrono::steady_clock and lpm - This code gets compiled if _POSIX_TIMERS is not set - */ - struct timer - { - //! 64-bit time - typedef unsigned long long Time; - - //! Create and start the timer - timer(); - //! Restart the counter - void restart(); - //! Return elapsed time in seconds - double elapsed() const; - - private: - //! Return time at call - Time curTime() const; - - Time _start_time; //! time when counter started - }; -} -#endif // _POSIX_TIMERS #endif // __POINTMATCHER_TIMER_H From 9b2055cfaa73a6cb812749a8aa5e129c747b8b40 Mon Sep 17 00:00:00 2001 From: boxanm Date: Sun, 12 Jan 2025 13:50:11 -0500 Subject: [PATCH 3/6] Add a CMake variable controlling the use of POSIX timers --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9dabfd0b..6d0b795c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -383,6 +383,11 @@ if (EXTRA_DEPS) add_dependencies(pointmatcher ${EXTRA_DEPS}) endif() +option(DISABLE_POSIX_TIMERS "Disable POSIX timers" OFF) +if(DISABLE_POSIX_TIMERS) + target_compile_definitions(pointmatcher PRIVATE FORCE_DISABLE_POSIX_TIMERS) +endif() + if (POSIX_TIMERS AND NOT APPLE) target_link_libraries(pointmatcher PRIVATE rt) endif () From 7fc4346c5a068d6474125344bad7cc2eaa0dace4 Mon Sep 17 00:00:00 2001 From: boxanm Date: Sun, 12 Jan 2025 13:51:28 -0500 Subject: [PATCH 4/6] Remove timer debug information --- pointmatcher/Timer.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pointmatcher/Timer.cpp b/pointmatcher/Timer.cpp index 1ec1345a..44fe979f 100644 --- a/pointmatcher/Timer.cpp +++ b/pointmatcher/Timer.cpp @@ -45,11 +45,6 @@ namespace PointMatcherSupport timer::timer(): _start_time(curTime()) { - #if defined(_POSIX_TIMERS) && !defined(FORCE_DISABLE_POSIX_TIMERS) - std::cout << "Using POSIX timer\n"; - #else - std::cout << "Using std::chrono timer\n"; - #endif } void timer::restart() From bbdeea982772f5c104c91702677518e0ec3f3cd5 Mon Sep 17 00:00:00 2001 From: boxanm Date: Sun, 12 Jan 2025 13:56:59 -0500 Subject: [PATCH 5/6] Add a Timer example script --- examples/CMakeLists.txt | 3 +++ examples/timer.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 examples/timer.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b79b7863..ab02fbb1 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -22,3 +22,6 @@ target_link_libraries(icp_advance_api pointmatcher) add_executable(icp_customized icp_customized.cpp) target_link_libraries(icp_customized pointmatcher) + +add_executable(timer timer.cpp) +target_link_libraries(timer pointmatcher) diff --git a/examples/timer.cpp b/examples/timer.cpp new file mode 100644 index 00000000..465b4a0f --- /dev/null +++ b/examples/timer.cpp @@ -0,0 +1,25 @@ +#include "pointmatcher/Timer.h" +#include +#include +#include + +namespace Time { + using Clock = std::chrono::high_resolution_clock; + using TimePoint = std::chrono::time_point; +} + +int main() { + const Time::TimePoint time1 = Time::Clock::now(); + PointMatcherSupport::timer t1; + + std::this_thread::sleep_for(std::chrono::seconds(10)); + + const Time::TimePoint time2 = Time::Clock::now(); + const double elapsed = t1.elapsed(); + + std::cout << "PM:timer time = " << elapsed << "\nchrono duration = " + << std::chrono::duration_cast(time2 - time1).count() / double(1000000000) + << " ms" << std::endl; + + return 0; +} From d04df362356d3afd632f5ba78c1ec5c2d46f45cb Mon Sep 17 00:00:00 2001 From: boxanm Date: Wed, 15 Jan 2025 09:32:18 -0500 Subject: [PATCH 6/6] Remove leftover commented code in Timer.cpp --- pointmatcher/Timer.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pointmatcher/Timer.cpp b/pointmatcher/Timer.cpp index 44fe979f..4397b7c0 100644 --- a/pointmatcher/Timer.cpp +++ b/pointmatcher/Timer.cpp @@ -81,13 +81,3 @@ namespace PointMatcherSupport } } // namespace PointMatcherSupport -// #else // _POSIX_TIMERS -// namespace PointMatcherSupport -// { - -// timer::Time timer::curTime() const -// { -// return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); -// } -// }// namespace PointMatcherSupport -// #endif // _POSIX_TIMERS