Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace boost::timer with std::chrono::steady_clock #598

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
25 changes: 25 additions & 0 deletions examples/timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "pointmatcher/Timer.h"
#include <iostream>
#include <chrono>
#include <thread>

namespace Time {
using Clock = std::chrono::high_resolution_clock;
using TimePoint = std::chrono::time_point<Clock>;
}

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<std::chrono::nanoseconds>(time2 - time1).count() / double(1000000000)
<< " ms" << std::endl;

return 0;
}
46 changes: 24 additions & 22 deletions pointmatcher/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,44 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <mach/mach.h>
#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::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
#endif // _POSIX_TIMERS

}
} // namespace PointMatcherSupport
#endif // _POSIX_TIMERS

31 changes: 14 additions & 17 deletions pointmatcher/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,33 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef __POINTMATCHER_TIMER_H
#define __POINTMATCHER_TIMER_H

#include <iostream>
#include <time.h>
#ifndef WIN32
#include <unistd.h>
#endif // WIN32
#if !defined(_POSIX_TIMERS) || defined(FORCE_DISABLE_POSIX_TIMERS)
#include <chrono>
#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
Expand All @@ -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 <boost/timer.hpp>
namespace PointMatcherSupport
{
typedef boost::timer timer;
}
#endif // _POSIX_TIMERS

#endif // __POINTMATCHER_TIMER_H