New version of the boost timer (addon lib).
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
#ifndef BOOST_TIMER_HPP
|
||||
#define BOOST_TIMER_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
// Speeds up compilation a bit on msvc
|
||||
#if (defined _MSC_VER) && (_MSC_VER >= 1200)
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4244)
|
||||
#endif
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include "boost/timer/implementation.hpp"
|
||||
#include "boost/timer/devices.hpp"
|
||||
#include "boost/timer/typedefs.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
||||
#endif // BOOST_TIMER_HPP
|
||||
@@ -1,31 +0,0 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_HPP
|
||||
#define BOOST_TIMER_DEVICES_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
|
||||
// -------------------------- Devices --------------------------
|
||||
|
||||
//! A device has 3 main member functions: start(), elapsed() & reset().
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include "boost/timer/devices/clock.hpp"
|
||||
#include "boost/timer/devices/date_time.hpp"
|
||||
|
||||
#ifndef BOOST_WINDOWS
|
||||
#include "boost/timer/devices/gettimeofday.hpp"
|
||||
#else
|
||||
#include "boost/timer/devices/QueryPerformanceCounter.hpp"
|
||||
#include "boost/timer/devices/timeGetTime.hpp"
|
||||
#include "boost/timer/devices/GetSystemTimeAsFileTime.hpp"
|
||||
#include "boost/timer/devices/GetTickCount.hpp"
|
||||
#endif
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_HPP
|
||||
@@ -1,107 +0,0 @@
|
||||
#ifndef BOOST_TIMER_IMPLEMENTATION_HPP
|
||||
#define BOOST_TIMER_IMPLEMENTATION_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//! The timer class takes a device type that's used for the elapsed time computation.
|
||||
//! Calling start(), pause() or resume() twice has no special effects.
|
||||
template<class Device>
|
||||
class timer
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef Device device_type;
|
||||
typedef typename device_type::time_duration_type time_duration_type;
|
||||
|
||||
enum start_options { auto_start = 1, manual_start };
|
||||
|
||||
//! By default, start the timer without any time offset
|
||||
explicit timer(time_duration_type initial_duration = time_duration_type(0, 0, 0), start_options start_op = auto_start)
|
||||
: m_elapsed(initial_duration), m_running(false)
|
||||
{
|
||||
if(start_op == auto_start)
|
||||
start();
|
||||
}
|
||||
|
||||
//! Starts the timer
|
||||
void start()
|
||||
{
|
||||
if(!m_running)
|
||||
{
|
||||
m_device.start();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
//! Restarts the timer
|
||||
void restart()
|
||||
{
|
||||
reset();
|
||||
start();
|
||||
}
|
||||
|
||||
//! Returns the elapsed time
|
||||
time_duration_type elapsed() const
|
||||
{
|
||||
return m_running ? (m_device.elapsed() + m_elapsed) : m_elapsed;
|
||||
}
|
||||
|
||||
//! Pauses the timer
|
||||
void pause()
|
||||
{
|
||||
if(m_running)
|
||||
{
|
||||
m_elapsed += m_device.elapsed();
|
||||
m_device.reset();
|
||||
m_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
//! Resumes the timer
|
||||
void resume()
|
||||
{
|
||||
if(!m_running)
|
||||
{
|
||||
m_device.start();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
//! Resets the timer
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_device.reset();
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
//! Tells wether the timer is currently running or not
|
||||
bool is_running() const
|
||||
{
|
||||
return m_running;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable device_type m_device;
|
||||
mutable time_duration_type m_elapsed;
|
||||
bool m_running;
|
||||
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_TIMER_IMPLEMENTATION_HPP
|
||||
@@ -1,55 +0,0 @@
|
||||
#ifndef BOOST_TIMER_TYPEDEFS_HPP
|
||||
#define BOOST_TIMER_TYPEDEFS_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
#include "boost/timer/implementation.hpp"
|
||||
#include "boost/timer/devices.hpp"
|
||||
|
||||
|
||||
// -------------------------- Devices --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
// boost::date_time devices
|
||||
typedef timer<microsec_device> microsec_timer;
|
||||
typedef timer<second_device> second_timer;
|
||||
|
||||
// std::clock() device
|
||||
typedef timer<clock_device> clock_timer;
|
||||
|
||||
#ifndef BOOST_WINDOWS
|
||||
|
||||
// gettimeofday() device
|
||||
typedef timer<gtod_device> gtod_timer;
|
||||
|
||||
#else
|
||||
|
||||
// QueryPerformanceCounter() device
|
||||
typedef timer<qpc_device> qpc_timer;
|
||||
|
||||
// timeGetTime() device
|
||||
typedef timer<tgt_device> tgt_timer;
|
||||
|
||||
// GetSystemTimeAsFileTime() device
|
||||
typedef timer<gstaft_device> gstaft_timer;
|
||||
|
||||
// GetTickCount() device
|
||||
typedef timer<gtc_device> gtc_timer;
|
||||
|
||||
#endif
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_TIMER_TYPEDEFS_HPP
|
||||
@@ -0,0 +1,29 @@
|
||||
#ifndef _BOOST_TIMERS_HPP
|
||||
#define _BOOST_TIMERS_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Speeds up compilation a bit on msvc
|
||||
#if (defined _MSC_VER) && (_MSC_VER >= 1200)
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
#include "timers/timer.hpp"
|
||||
#include "timers/portable.hpp"
|
||||
|
||||
#ifdef BOOST_WINDOWS
|
||||
#include "timers/win32.hpp"
|
||||
#else
|
||||
#include "timers/posix.hpp"
|
||||
#endif
|
||||
|
||||
#endif // _BOOST_TIMERS_HPP
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef _BOOST_TIMERS_PORTABLE_HPP
|
||||
#define _BOOST_TIMERS_PORTABLE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "portable/clock_device.hpp"
|
||||
#include "portable/date_time_devices.hpp"
|
||||
#include "portable/time_device.hpp"
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace portable
|
||||
{
|
||||
|
||||
// boost::date_time devices
|
||||
typedef timer<microsec_device> microsec_timer;
|
||||
typedef timer<second_device> second_timer;
|
||||
|
||||
// std::clock() device
|
||||
typedef timer<clock_device> clock_timer;
|
||||
|
||||
// std::time() device
|
||||
typedef timer<time_device> time_timer;
|
||||
|
||||
// Typedef which timer is the best
|
||||
typedef microsec_timer best_timer;
|
||||
|
||||
} // namespace portable
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_PORTABLE_HPP
|
||||
+84
-85
@@ -1,85 +1,84 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_CLOCK_HPP
|
||||
#define BOOST_TIMER_DEVICES_CLOCK_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
// -------------------------- std::clock() --------------------------
|
||||
|
||||
// Properties:
|
||||
// - ??? ns resolution.
|
||||
// - ??? ns overhead.
|
||||
// - Is implemented with GetSystemTimeAsFileTime on windows.
|
||||
//
|
||||
// Pros:
|
||||
// - Portable.
|
||||
//
|
||||
// Cons:
|
||||
// - High resolution.
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Standard headers
|
||||
#include <ctime>
|
||||
|
||||
// Boost headers
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//! The clock_device class is based on std::clock() to compute the time
|
||||
class clock_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit clock_device() : m_start(0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start)
|
||||
m_start = std::clock();
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start)
|
||||
{
|
||||
std::clock_t current = std::clock();
|
||||
m_elapsed += posix_time::milliseconds(static_cast<double>(current-m_start) / CLOCKS_PER_SEC * 1000.0);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable std::clock_t m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_CLOCK_HPP
|
||||
#ifndef _BOOST_TIMERS_PORTABLE_CLOCK_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_PORTABLE_CLOCK_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Standard headers
|
||||
#include <ctime>
|
||||
|
||||
// Boost headers
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace portable
|
||||
{
|
||||
|
||||
//! The clock_device class is based on std::clock() to compute the time
|
||||
class clock_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit clock_device() : m_running(false), m_start(0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_running)
|
||||
{
|
||||
m_start = std::clock();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_running)
|
||||
{
|
||||
std::clock_t current = std::clock();
|
||||
double seconds = (current - m_start) / static_cast<double>(CLOCKS_PER_SEC);
|
||||
m_elapsed += posix_time::milliseconds(static_cast<int64_t>(seconds * 1000.0));
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start = 0;
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool m_running;
|
||||
mutable std::clock_t m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace portable
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_PORTABLE_CLOCK_DEVICE_HPP
|
||||
+80
-84
@@ -1,84 +1,80 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_DATE_TIME_HPP
|
||||
#define BOOST_TIMER_DEVICES_DATE_TIME_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
|
||||
// -------------------------- date_time devices --------------------------
|
||||
|
||||
// Properties:
|
||||
// - windows: uses GetSystemTimeAsFileTime()
|
||||
// - POSIX: uses gettimeofday()
|
||||
//
|
||||
// Pros:
|
||||
// - Portable.
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//! The date_time device
|
||||
template <class Clock, class Time = posix_time::ptime>
|
||||
class date_time_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef Clock clock_type;
|
||||
typedef Time time_type;
|
||||
typedef typename clock_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit date_time_device() : m_start(date_time::not_a_date_time), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(m_start.is_not_a_date_time())
|
||||
m_start = clock_type::local_time();
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(!m_start.is_not_a_date_time())
|
||||
{
|
||||
time_type current(clock_type::local_time());
|
||||
m_elapsed += (current - m_start);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_start = date_time::not_a_date_time;
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable time_type m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
// Convenience typedefs
|
||||
typedef date_time_device<posix_time::microsec_clock> microsec_device;
|
||||
typedef date_time_device<posix_time::second_clock> second_device;
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_DATE_TIME_HPP
|
||||
#ifndef _BOOST_TIMERS_PORTABLE_DATE_TIME_DEVICES_HPP
|
||||
#define _BOOST_TIMERS_PORTABLE_DATE_TIME_DEVICES_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace portable
|
||||
{
|
||||
|
||||
//! The date_time device
|
||||
template <class Clock, class Time = posix_time::ptime>
|
||||
class date_time_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef Clock clock_type;
|
||||
typedef Time time_type;
|
||||
typedef typename clock_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit date_time_device() : m_start(date_time::not_a_date_time), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(m_start.is_not_a_date_time())
|
||||
m_start = clock_type::local_time();
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(!m_start.is_not_a_date_time())
|
||||
{
|
||||
time_type current(clock_type::local_time());
|
||||
m_elapsed += (current - m_start);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_start = date_time::not_a_date_time;
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable time_type m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
// Convenience typedefs
|
||||
typedef date_time_device<posix_time::microsec_clock> microsec_device;
|
||||
typedef date_time_device<posix_time::second_clock> second_device;
|
||||
|
||||
} // namespace portable
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_PORTABLE_DATE_TIME_DEVICES_HPP
|
||||
@@ -0,0 +1,76 @@
|
||||
#ifndef _BOOST_TIMERS_PORTABLE_TIME_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_PORTABLE_TIME_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Standard headers
|
||||
#include <ctime>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace portable
|
||||
{
|
||||
|
||||
//! The time_device class is based on std::time() to compute the time
|
||||
class time_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit time_device() : m_start(0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start)
|
||||
m_start = std::time(0);
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start)
|
||||
{
|
||||
std::time_t current = std::time(0);
|
||||
long seconds = static_cast<long>(current - m_start);
|
||||
m_elapsed += posix_time::seconds(seconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable std::time_t m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace portable
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_PORTABLE_TIME_DEVICE_HPP
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef _BOOST_TIMERS_POSIX_HPP
|
||||
#define _BOOST_TIMERS_POSIX_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "posix/ftime_device.hpp"
|
||||
#include "posix/gru_device.hpp"
|
||||
#include "posix/gtod_device.hpp"
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace posix
|
||||
{
|
||||
|
||||
// ftime() device
|
||||
typedef timer<ftime_device> ftime_timer;
|
||||
|
||||
// getcputime() device
|
||||
typedef timer<gru_device> gru_timer;
|
||||
|
||||
// gettimeofday() device
|
||||
typedef timer<gtod_device> gtod_timer;
|
||||
|
||||
// Typedef which timer is the best
|
||||
typedef gtod_timer best_timer;
|
||||
|
||||
} // namespace posix
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_POSIX_HPP
|
||||
@@ -0,0 +1,86 @@
|
||||
#ifndef _BOOST_TIMERS_POSIX_FTIME_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_POSIX_FTIME_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Boost headers
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Linux headers
|
||||
#include <sys/timeb.h>
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace posix
|
||||
{
|
||||
|
||||
//! The gtod_device class is based on gettimeofday() to compute the time
|
||||
class ftime_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit ftime_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
std::memset(&m_start, 0, sizeof(timeb));
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!is_time(m_start))
|
||||
ftime(&m_start);
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(is_time(m_start))
|
||||
{
|
||||
timeb current;
|
||||
ftime(¤t);
|
||||
int64_t seconds = current.time - m_start.time;
|
||||
int64_t milliseconds = current.millitm - m_start.millitm;
|
||||
m_elapsed += posix_time::milliseconds(seconds * 1000 + milliseconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
std::memset(&m_start, 0, sizeof(timeb));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool is_time(const timeb& value) const
|
||||
{
|
||||
return value.time != 0;
|
||||
}
|
||||
|
||||
mutable timeb m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace posix
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_POSIX_FTIME_DEVICE_HPP
|
||||
@@ -0,0 +1,88 @@
|
||||
#ifndef _BOOST_TIMERS_POSIX_GRU_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_POSIX_GRU_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Boost headers
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Linux headers
|
||||
#include <sys/times.h>
|
||||
#include <sys/resource.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace posix
|
||||
{
|
||||
|
||||
//! The gru_device class is based on getrusage() to compute the time
|
||||
class gru_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gru_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
std::memset(&m_start, 0, sizeof(timeval));
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!is_time(m_start))
|
||||
getrusage(RUSAGE_SELF, &m_start);
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(is_time(m_start))
|
||||
{
|
||||
rusage current;
|
||||
getrusage(RUSAGE_SELF, ¤t);
|
||||
int64_t seconds = current.ru_utime.tv_sec - m_start.ru_utime.tv_sec;
|
||||
int64_t microseconds = current.ru_utime.tv_usec - m_start.ru_utime.tv_usec;
|
||||
m_elapsed += posix_time::microseconds(seconds * 1000000 + microseconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
std::memset(&m_start, 0, sizeof(timeval));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool is_time(const rusage& value) const
|
||||
{
|
||||
return value.ru_utime.tv_sec || value.ru_utime.tv_usec;
|
||||
}
|
||||
|
||||
mutable rusage m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace posix
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_POSIX_GRU_DEVICE_HPP
|
||||
+86
-98
@@ -1,98 +1,86 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_GTOD_HPP
|
||||
#define BOOST_TIMER_DEVICES_GTOD_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
// -------------------------- gettimeofday() --------------------------
|
||||
|
||||
// Properties:
|
||||
// - ??? ns resolution.
|
||||
// - ??? ns overhead.
|
||||
//
|
||||
// Pros:
|
||||
// - Low overhead.
|
||||
// - High resolution.
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Get config
|
||||
#include <boost/config.hpp>
|
||||
|
||||
// Check we have gettimeofday()
|
||||
#ifndef BOOST_HAS_GETTIMEOFDAY
|
||||
#error "gettimeofday() can only be used on POSIX systems"
|
||||
#else
|
||||
|
||||
// Boost headers
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Linux headers
|
||||
#include <sys/time.h>
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//! The gtod_device class is based on gettimeofday() to compute the time
|
||||
class gtod_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gtod_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
std::memset(&m_start, 0, sizeof(timeval));
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!is_time(m_start))
|
||||
gettimeofday(&m_start, 0);
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(is_time(m_start))
|
||||
{
|
||||
timeval current;
|
||||
gettimeofday(¤t, 0);
|
||||
m_elapsed += posix_time::milliseconds((current.tv_sec-m_start.tv_sec) * 1000 + ((current.tv_usec-m_start.tv_usec) / 1000.0));
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
std::memset(&m_start, 0, sizeof(timeval));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool is_time(const timeval& value) const
|
||||
{
|
||||
return value.tv_sec || value.tv_usec;
|
||||
}
|
||||
|
||||
mutable timeval m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_HAS_GETTIMEOFDAY
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_GTOD_HPP
|
||||
#ifndef _BOOST_TIMERS_POSIX_GTOD_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_POSIX_GTOD_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Boost headers
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Linux headers
|
||||
#include <sys/time.h>
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace posix
|
||||
{
|
||||
|
||||
//! The gtod_device class is based on gettimeofday() to compute the time
|
||||
class gtod_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gtod_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
std::memset(&m_start, 0, sizeof(timeval));
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!is_time(m_start))
|
||||
gettimeofday(&m_start, 0);
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(is_time(m_start))
|
||||
{
|
||||
timeval current;
|
||||
gettimeofday(¤t, 0);
|
||||
int64_t seconds = current.tv_sec - m_start.tv_sec;
|
||||
int64_t microseconds = current.tv_usec - m_start.tv_usec;
|
||||
m_elapsed += posix_time::microseconds(seconds * 1000000 + microseconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
std::memset(&m_start, 0, sizeof(timeval));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool is_time(const timeval& value) const
|
||||
{
|
||||
return value.tv_sec || value.tv_usec;
|
||||
}
|
||||
|
||||
mutable timeval m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace posix
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_POSIX_GTOD_DEVICE_HPP
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifndef _BOOST_TIMERS_TIMER_HPP
|
||||
#define _BOOST_TIMERS_TIMER_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
namespace timers
|
||||
{
|
||||
|
||||
//! The timer class takes a device type that's used for the elapsed time computation.
|
||||
//! Calling start(), pause() or resume() twice has no special effects.
|
||||
template<class Device>
|
||||
class timer
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef Device device_type;
|
||||
typedef typename device_type::time_duration_type time_duration_type;
|
||||
|
||||
enum start_options { auto_start = 1, manual_start };
|
||||
|
||||
//! By default, start the timer without any time offset
|
||||
explicit timer(time_duration_type initial_duration = time_duration_type(0, 0, 0), start_options start_op = auto_start)
|
||||
: m_elapsed(initial_duration), m_running(false)
|
||||
{
|
||||
if(start_op == auto_start)
|
||||
start();
|
||||
}
|
||||
|
||||
//! Starts the timer
|
||||
void start()
|
||||
{
|
||||
if(!m_running)
|
||||
{
|
||||
m_device.start();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
//! Restarts the timer
|
||||
void restart()
|
||||
{
|
||||
reset();
|
||||
start();
|
||||
}
|
||||
|
||||
//! Returns the elapsed time
|
||||
time_duration_type elapsed() const
|
||||
{
|
||||
return m_running ? (m_device.elapsed() + m_elapsed) : m_elapsed;
|
||||
}
|
||||
|
||||
//! Pauses the timer
|
||||
void pause()
|
||||
{
|
||||
if(m_running)
|
||||
{
|
||||
m_elapsed += m_device.elapsed();
|
||||
m_device.reset();
|
||||
m_running = false;
|
||||
}
|
||||
}
|
||||
|
||||
//! Resumes the timer
|
||||
void resume()
|
||||
{
|
||||
if(!m_running)
|
||||
{
|
||||
m_device.start();
|
||||
m_running = true;
|
||||
}
|
||||
}
|
||||
|
||||
//! Resets the timer
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_device.reset();
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
//! Tells wether the timer is currently running or not
|
||||
bool is_running() const
|
||||
{
|
||||
return m_running;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable device_type m_device;
|
||||
mutable time_duration_type m_elapsed;
|
||||
bool m_running;
|
||||
|
||||
};
|
||||
|
||||
} // namespace timers
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_TIMER_HPP
|
||||
@@ -0,0 +1,57 @@
|
||||
#ifndef _BOOST_TIMERS_WIN32_HPP
|
||||
#define _BOOST_TIMERS_WIN32_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#include "timer.hpp"
|
||||
#include "win32/gpt_device.hpp"
|
||||
#include "win32/gtc_device.hpp"
|
||||
#include "win32/gtt_device.hpp"
|
||||
#include "win32/gstaft_device.hpp"
|
||||
#include "win32/qpc_device.hpp"
|
||||
#include "win32/tgt_device.hpp"
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
// GetProcessTimes() device
|
||||
typedef timer<gpt_device> gpt_timer;
|
||||
|
||||
// GetThreadTimes() device
|
||||
typedef timer<gtt_device> gtt_timer;
|
||||
|
||||
// GetTickCount() device
|
||||
typedef timer<gtc_device> gtc_timer;
|
||||
|
||||
// GetSystemTimeAsFileTime() device
|
||||
typedef timer<gstaft_device> gstaft_timer;
|
||||
|
||||
// QueryPerformanceCounter() device
|
||||
typedef timer<qpc_device> qpc_timer;
|
||||
|
||||
// timeGetTime() device
|
||||
typedef timer<tgt_device> tgt_timer;
|
||||
|
||||
// Typedef which timer is the best
|
||||
typedef qpc_timer best_timer;
|
||||
|
||||
} // namespace win32
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_WIN32_HPP
|
||||
@@ -0,0 +1,97 @@
|
||||
#ifndef _BOOST_TIMERS_WIN32_GPT_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_WIN32_GPT_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Standard headers
|
||||
#include <utility>
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
//! The gtt_device class is based on GetThreadTimes() to compute the time
|
||||
class gpt_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gpt_device() : m_start(0, 0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!is_time(m_start))
|
||||
{
|
||||
FILETIME tmp1, tmp2;
|
||||
GetProcessTimes(GetCurrentProcess(), &tmp1, &tmp2, reinterpret_cast<LPFILETIME>(&m_start.first), reinterpret_cast<LPFILETIME>(&m_start.second));
|
||||
}
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(is_time(m_start))
|
||||
{
|
||||
std::pair<uint64_t, uint64_t> current;
|
||||
FILETIME tmp1, tmp2;
|
||||
GetProcessTimes(GetCurrentProcess(), &tmp1, &tmp2, reinterpret_cast<LPFILETIME>(¤t.first), reinterpret_cast<LPFILETIME>(¤t.second));
|
||||
// diff is measured in 100ns intervals...
|
||||
uint64_t diff = (current.first - m_start.first) + (current.second - m_start.second);
|
||||
// ... so we must divide by 10 to get nanoseconds
|
||||
uint64_t nanoseconds = static_cast<uint64_t>(diff / 10.0);
|
||||
m_elapsed += posix_time::milliseconds(static_cast<int64_t>(nanoseconds / 1000.0));
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start.first = m_start.second = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool is_time(const std::pair<uint64_t, uint64_t>& time) const
|
||||
{
|
||||
return (time.first != 0 || time.second != 0);
|
||||
}
|
||||
|
||||
mutable std::pair<uint64_t, uint64_t> m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_WIN32_GTT_DEVICE_HPP
|
||||
+86
-96
@@ -1,96 +1,86 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_GSTAFT_HPP
|
||||
#define BOOST_TIMER_DEVICES_GSTAFT_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
|
||||
// -------------------------- GetSystemTimeAsFileTime() --------------------------
|
||||
|
||||
// Properties:
|
||||
// - ??? ns resolution.
|
||||
// - ??? ns overhead (6 clocks (64-bit load/store + synchronization (4))).
|
||||
//
|
||||
// Pros:
|
||||
// - Low overhead.
|
||||
//
|
||||
// Cons:
|
||||
// - Low resolution.
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#ifndef BOOST_WINDOWS
|
||||
#error "GetSystemTimeAsFileTime() can only be used on windows"
|
||||
#else
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//! The gstaft_device class is based on GetSystemTimeAsFileTime() to compute the time
|
||||
class gstaft_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gstaft_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
std::memset(&m_start, 0, sizeof(ULARGE_INTEGER));
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start.QuadPart)
|
||||
GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(&m_start));
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start.QuadPart)
|
||||
{
|
||||
ULARGE_INTEGER current;
|
||||
GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(¤t));
|
||||
uint64_t tmp = current.QuadPart - m_start.QuadPart;
|
||||
m_elapsed += posix_time::milliseconds(tmp / 10000.0);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
std::memset(&m_start, 0, sizeof(ULARGE_INTEGER));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable ULARGE_INTEGER m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_WINDOWS
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_GSTAFT_HPP
|
||||
#ifndef _BOOST_TIMERS_WIN32_GSTAFT_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_WIN32_GSTAFT_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
//! The gstaft_device class is based on GetSystemTimeAsFileTime() to compute the time
|
||||
class gstaft_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gstaft_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
std::memset(&m_start, 0, sizeof(ULARGE_INTEGER));
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start.QuadPart)
|
||||
GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(&m_start));
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start.QuadPart)
|
||||
{
|
||||
ULARGE_INTEGER current;
|
||||
GetSystemTimeAsFileTime(reinterpret_cast<LPFILETIME>(¤t));
|
||||
// diff is measured in 100ns intervals...
|
||||
uint64_t diff = current.QuadPart - m_start.QuadPart;
|
||||
// ... so we must divide by 10 to get nanoseconds
|
||||
uint64_t nanoseconds = static_cast<uint64_t>(diff / 10.0);
|
||||
m_elapsed += posix_time::milliseconds(static_cast<int64_t>(nanoseconds / 1000.0));
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
std::memset(&m_start, 0, sizeof(ULARGE_INTEGER));
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable ULARGE_INTEGER m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_WIN32_GSTAFT_DEVICE_HPP
|
||||
+81
-92
@@ -1,92 +1,81 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_GTC_HPP
|
||||
#define BOOST_TIMER_DEVICES_GTC_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
// -------------------------- GetTickCount() --------------------------
|
||||
|
||||
// Properties:
|
||||
// - 5 to 55 ms resolution.
|
||||
// - ??? ns overhead (8 clock (load, mul, shift)).
|
||||
//
|
||||
// Pros:
|
||||
// - Very low overhead.
|
||||
//
|
||||
// Cons:
|
||||
// - Very low resolution.
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#ifndef BOOST_WINDOWS
|
||||
#error "GetTickCount() can only be used on windows"
|
||||
#else
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//! The gtc_device class is based on GetTickCount() to compute the time
|
||||
class gtc_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gtc_device() : m_start(0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start)
|
||||
m_start = GetTickCount();
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start)
|
||||
{
|
||||
uint32_t current = GetTickCount();
|
||||
m_elapsed += posix_time::milliseconds(current - m_start);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable uint32_t m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_WINDOWS
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_GTC_HPP
|
||||
#ifndef _BOOST_TIMERS_WIN32_GTC_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_WIN32_GTC_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
//! The gtc_device class is based on GetTickCount() to compute the time
|
||||
class gtc_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gtc_device() : m_start(0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start)
|
||||
m_start = GetTickCount();
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start)
|
||||
{
|
||||
uint32_t current = GetTickCount();
|
||||
uint64_t milliseconds = current - m_start;
|
||||
m_elapsed += posix_time::milliseconds(milliseconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable uint32_t m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_WIN32_GTC_DEVICE_HPP
|
||||
@@ -0,0 +1,97 @@
|
||||
#ifndef _BOOST_TIMERS_WIN32_GTT_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_WIN32_GTT_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Standard headers
|
||||
#include <utility>
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
//! The gtt_device class is based on GetThreadTimes() to compute the time
|
||||
class gtt_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit gtt_device() : m_start(0, 0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!is_time(m_start))
|
||||
{
|
||||
FILETIME tmp1, tmp2;
|
||||
GetThreadTimes(GetCurrentThread(), &tmp1, &tmp2, reinterpret_cast<LPFILETIME>(&m_start.first), reinterpret_cast<LPFILETIME>(&m_start.second));
|
||||
}
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(is_time(m_start))
|
||||
{
|
||||
std::pair<uint64_t, uint64_t> current;
|
||||
FILETIME tmp1, tmp2;
|
||||
GetThreadTimes(GetCurrentThread(), &tmp1, &tmp2, reinterpret_cast<LPFILETIME>(¤t.first), reinterpret_cast<LPFILETIME>(¤t.second));
|
||||
// diff is measured in 100ns intervals...
|
||||
uint64_t diff = (current.first - m_start.first) + (current.second - m_start.second);
|
||||
// ... so we must divide by 10 to get nanoseconds
|
||||
uint64_t nanoseconds = static_cast<uint64_t>(diff / 10.0);
|
||||
m_elapsed += posix_time::milliseconds(static_cast<int64_t>(nanoseconds / 1000.0));
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start.first = m_start.second = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
bool is_time(const std::pair<uint64_t, uint64_t>& time) const
|
||||
{
|
||||
return (time.first != 0 || time.second != 0);
|
||||
}
|
||||
|
||||
mutable std::pair<uint64_t, uint64_t> m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_WIN32_GTT_DEVICE_HPP
|
||||
+104
-130
@@ -1,130 +1,104 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_QPC_HPP
|
||||
#define BOOST_TIMER_DEVICES_QPC_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
|
||||
// -------------------------- QueryPerformanceCounter() --------------------------
|
||||
|
||||
// Properties:
|
||||
// Win2k (PIT):
|
||||
// - 838 ns resolution.
|
||||
// - ~3000 ns overhead.
|
||||
// WinXP (PMT):
|
||||
// - 279 ns resolution.
|
||||
// - 700 ns overhead.
|
||||
//
|
||||
// Pros:
|
||||
// - High resolution (usually the best one).
|
||||
//
|
||||
// Cons:
|
||||
// - High overhead.
|
||||
// - Not thread safe.
|
||||
// - Multi core problems. To solve the issue you can #define BOOST_QPC_SET_AFFINITY which will call SetThreadAffinityMask() for you.
|
||||
//
|
||||
// Issues:
|
||||
// 1) Q274323: may jump several seconds under heavy PCI bus load.
|
||||
// not a problem, because the older systems on which this occurs
|
||||
// have safe TSCs, so that is used instead.
|
||||
// 2) "System clock problem can inflate benchmark scores":
|
||||
// incorrect value if not polled every 4.5 seconds?
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#ifndef BOOST_WINDOWS
|
||||
#error "QueryPerformanceCounter() can only be used on windows"
|
||||
#else
|
||||
|
||||
// Boost headers
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
class qpc_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit qpc_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
#ifdef BOOST_QPC_SET_AFFINITY
|
||||
m_affinity = SetThreadAffinityMask(GetCurrentThread(), 1);
|
||||
#endif
|
||||
|
||||
m_start.QuadPart = 0;
|
||||
if(!QueryPerformanceFrequency(&m_frequency))
|
||||
throw_exception(std::runtime_error("qpc_device: QueryPerformanceFrequency() indicated there's no high-resolution performance counter"));
|
||||
|
||||
// Make it so we have the future results directly in milliseconds
|
||||
m_frequency.QuadPart /= 1000;
|
||||
}
|
||||
|
||||
~qpc_device()
|
||||
{
|
||||
#ifdef BOOST_QPC_SET_AFFINITY
|
||||
SetThreadAffinityMask(GetCurrentThread(), m_affinity);
|
||||
#endif
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start.QuadPart && !QueryPerformanceCounter(&m_start))
|
||||
throw_exception(std::runtime_error("qpc_device: QueryPerformanceCounter() failed"));
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start.QuadPart)
|
||||
{
|
||||
LARGE_INTEGER current;
|
||||
if(!QueryPerformanceCounter(¤t))
|
||||
throw_exception(std::runtime_error("qpc_device: QueryPerformanceCounter() failed"));
|
||||
uint64_t milliseconds = (current.QuadPart - m_start.QuadPart) / m_frequency.QuadPart;
|
||||
m_elapsed += posix_time::milliseconds(milliseconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start.QuadPart = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
LARGE_INTEGER m_frequency;
|
||||
mutable LARGE_INTEGER m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
#ifdef BOOST_QPC_SET_AFFINITY
|
||||
DWORD_PTR m_affinity;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_WINDOWS
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_QPC_HPP
|
||||
#ifndef _BOOST_TIMERS_WIN32_QPC_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_WIN32_QPC_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Boost headers
|
||||
#include <boost/throw_exception.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
class qpc_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit qpc_device() : m_elapsed(0, 0, 0)
|
||||
{
|
||||
#ifdef BOOST_QPC_SET_AFFINITY
|
||||
m_affinity = SetThreadAffinityMask(GetCurrentThread(), 1);
|
||||
#endif
|
||||
|
||||
m_start.QuadPart = 0;
|
||||
if(!QueryPerformanceFrequency(&m_frequency))
|
||||
throw_exception(std::runtime_error("qpc_device: QueryPerformanceFrequency() indicated there's no high-resolution performance counter"));
|
||||
|
||||
// Make it so we have the future results directly in milliseconds
|
||||
m_frequency.QuadPart /= 1000;
|
||||
}
|
||||
|
||||
~qpc_device()
|
||||
{
|
||||
#ifdef BOOST_QPC_SET_AFFINITY
|
||||
SetThreadAffinityMask(GetCurrentThread(), m_affinity);
|
||||
#endif
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start.QuadPart && !QueryPerformanceCounter(&m_start))
|
||||
throw_exception(std::runtime_error("qpc_device: QueryPerformanceCounter() failed"));
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start.QuadPart)
|
||||
{
|
||||
LARGE_INTEGER current;
|
||||
if(!QueryPerformanceCounter(¤t))
|
||||
throw_exception(std::runtime_error("qpc_device: QueryPerformanceCounter() failed"));
|
||||
int64_t milliseconds = (current.QuadPart - m_start.QuadPart) / m_frequency.QuadPart;
|
||||
m_elapsed += posix_time::milliseconds(milliseconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start.QuadPart = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
LARGE_INTEGER m_frequency;
|
||||
mutable LARGE_INTEGER m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
#ifdef BOOST_QPC_SET_AFFINITY
|
||||
DWORD_PTR m_affinity;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_WIN32_QUERYPERFORMANCECOUNTER_HPP
|
||||
+95
-94
@@ -1,94 +1,95 @@
|
||||
#ifndef BOOST_TIMER_DEVICES_TGT_HPP
|
||||
#define BOOST_TIMER_DEVICES_TGT_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2006
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2006
|
||||
|
||||
// -------------------------- std::clock() --------------------------
|
||||
|
||||
// Properties:
|
||||
// - 1 ms resolution.
|
||||
// - ??? ns overhead.
|
||||
//
|
||||
// Pros:
|
||||
// - Low overhead.
|
||||
//
|
||||
// Cons:
|
||||
// - Low resolution.
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
#ifndef BOOST_WINDOWS
|
||||
#error "timeGetTime() can only be used on windows"
|
||||
#else
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
// Automagically link winmm.lib if we can
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
|
||||
//! The tgt_device class is based on timeGetTime() to compute the time
|
||||
class tgt_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit tgt_device() : m_start(0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start)
|
||||
m_start = timeGetTime();
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start)
|
||||
{
|
||||
uint32_t current = timeGetTime();
|
||||
m_elapsed += posix_time::milliseconds(current - m_start);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable uint32_t m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace boost
|
||||
|
||||
#endif // BOOST_WINDOWS
|
||||
|
||||
#endif // BOOST_TIMER_DEVICES_TGT_HPP
|
||||
#ifndef _BOOST_TIMERS_WIN32_TGT_DEVICE_HPP
|
||||
#define _BOOST_TIMERS_WIN32_TGT_DEVICE_HPP
|
||||
|
||||
// (C) Copyright Vaucher Philippe 2007
|
||||
|
||||
// Use, modification and distribution is subject to the
|
||||
// Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
||||
// Author: Vaucher Philippe 2007
|
||||
|
||||
|
||||
// -------------------------- Includes --------------------------
|
||||
|
||||
// Boost headers
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||
|
||||
// Windows headers
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
// Automagically link winmm.lib if we can
|
||||
#if (defined _MSC_VER) && (_MSC_VER >= 1200)
|
||||
#pragma comment(lib, "winmm.lib")
|
||||
#endif
|
||||
|
||||
// -------------------------- Code --------------------------
|
||||
|
||||
namespace boost
|
||||
{
|
||||
namespace timers
|
||||
{
|
||||
namespace win32
|
||||
{
|
||||
|
||||
//! The tgt_device class is based on timeGetTime() to compute the time
|
||||
class tgt_device
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
typedef posix_time::ptime time_type;
|
||||
typedef time_type::time_duration_type time_duration_type;
|
||||
|
||||
explicit tgt_device() : m_start(0), m_elapsed(0, 0, 0)
|
||||
{
|
||||
#ifndef BOOST_TGT_DEFAULT_RES
|
||||
timeBeginPeriod(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
~tgt_device()
|
||||
{
|
||||
#ifndef BOOST_TGT_DEFAULT_RES
|
||||
timeEndPeriod(1);
|
||||
#endif
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
if(!m_start)
|
||||
m_start = timeGetTime();
|
||||
}
|
||||
|
||||
const time_duration_type& elapsed() const
|
||||
{
|
||||
if(m_start)
|
||||
{
|
||||
uint32_t current = timeGetTime();
|
||||
int64_t milliseconds = current - m_start;
|
||||
m_elapsed += posix_time::milliseconds(milliseconds);
|
||||
m_start = current;
|
||||
}
|
||||
return m_elapsed;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_elapsed = time_duration_type(0,0,0);
|
||||
m_start = 0;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
mutable uint32_t m_start;
|
||||
mutable time_duration_type m_elapsed;
|
||||
|
||||
};
|
||||
|
||||
} // namespace win32
|
||||
} // namespace timers
|
||||
} // namespace boost
|
||||
|
||||
#endif // _BOOST_TIMERS_WIN32_TGT_DEVICE_HPP
|
||||
Reference in New Issue
Block a user