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 () 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; +} diff --git a/pointmatcher/Timer.cpp b/pointmatcher/Timer.cpp index b7fee491..4397b7c0 100644 --- a/pointmatcher/Timer.cpp +++ b/pointmatcher/Timer.cpp @@ -40,42 +40,44 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #endif -#ifdef _POSIX_TIMERS namespace PointMatcherSupport { timer::timer(): _start_time(curTime()) { - } - + } + 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 - 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 -#endif // _POSIX_TIMERS - diff --git a/pointmatcher/Timer.h b/pointmatcher/Timer.h index 316dbe28..7b6d17e7 100644 --- a/pointmatcher/Timer.h +++ b/pointmatcher/Timer.h @@ -36,29 +36,33 @@ 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 +#if !defined(_POSIX_TIMERS) || defined(FORCE_DISABLE_POSIX_TIMERS) +#include +#endif -#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 + This is an interface between std::chrono::steady_clock and lpm + + 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 { //! 64-bit time typedef unsigned long long Time; - + //! Create and start the timer timer(); //! Restart the counter @@ -69,16 +73,9 @@ namespace PointMatcherSupport private: //! Return time at call Time curTime() const; - + Time _start_time; //! time when counter started }; } // namespace PointMatcherSupport -#else // _POSIX_TIMERS -#include -namespace PointMatcherSupport -{ - typedef boost::timer timer; -} -#endif // _POSIX_TIMERS #endif // __POINTMATCHER_TIMER_H