Skip to content

Commit

Permalink
Merge pull request #2919 from SunBlack/use_chrono_in_time
Browse files Browse the repository at this point in the history
Use std::chrono instead of boost::posix_time in time.h
  • Loading branch information
SergioRAgostinho authored Mar 16, 2019
2 parents dd686b4 + cc1b6c1 commit 9470590
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#pragma once

#include <iomanip>
#include <string>

#include <QGLWidget>
Expand Down
43 changes: 14 additions & 29 deletions common/include/pcl/common/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,10 @@

#pragma once

#ifdef __GNUC__
#pragma GCC system_header
#endif

#include <cmath>
#include <chrono>
#include <iostream>
#include <queue>
#include <string>
#ifndef Q_MOC_RUN
#include <boost/date_time/posix_time/posix_time.hpp>
#endif

/**
* \file pcl/common/time.h
Expand All @@ -65,37 +59,37 @@ namespace pcl
{
public:
/** \brief Constructor. */
StopWatch () : start_time_ (boost::posix_time::microsec_clock::local_time ())
StopWatch () : start_time_ (std::chrono::steady_clock::now())
{
}

/** \brief Destructor. */
virtual ~StopWatch () {}
virtual ~StopWatch () = default;

/** \brief Retrieve the time in milliseconds spent since the last call to \a reset(). */
inline double
getTime ()
getTime () const
{
boost::posix_time::ptime end_time = boost::posix_time::microsec_clock::local_time ();
return (static_cast<double> (((end_time - start_time_).total_milliseconds ())));
auto end_time = std::chrono::steady_clock::now();
return std::chrono::duration<double, std::ratio<1, 1000>>(end_time - start_time_).count();
}

/** \brief Retrieve the time in seconds spent since the last call to \a reset(). */
inline double
getTimeSeconds ()
getTimeSeconds () const
{
return (getTime () * 0.001f);
return (getTime () * 0.001);
}

/** \brief Reset the stopwatch to 0. */
inline void
reset ()
{
start_time_ = boost::posix_time::microsec_clock::local_time ();
start_time_ = std::chrono::steady_clock::now();
}

protected:
boost::posix_time::ptime start_time_;
std::chrono::time_point<std::chrono::steady_clock> start_time_;
};

/** \brief Class to measure the time spent in a scope
Expand All @@ -116,16 +110,9 @@ namespace pcl
class ScopeTime : public StopWatch
{
public:
inline ScopeTime (const char* title) :
title_ (std::string (title))
{
start_time_ = boost::posix_time::microsec_clock::local_time ();
}

inline ScopeTime () :
title_ (std::string (""))
inline ScopeTime (const std::string &title = "") :
title_ (title)
{
start_time_ = boost::posix_time::microsec_clock::local_time ();
}

inline ~ScopeTime ()
Expand Down Expand Up @@ -203,9 +190,7 @@ namespace pcl
inline double
getTime ()
{
boost::posix_time::ptime epoch_time (boost::gregorian::date (1970, 1, 1));
boost::posix_time::ptime current_time = boost::posix_time::microsec_clock::local_time ();
return (static_cast<double>((current_time - epoch_time).total_nanoseconds ()) * 1.0e-9);
return std::chrono::duration<double>(std::chrono::system_clock::now().time_since_epoch()).count();
}

/// Executes code, only if secs are gone since last exec.
Expand Down

0 comments on commit 9470590

Please sign in to comment.