C++ Timer Klasse
C++ Timer class
Anmerkung: std >= C++11
: STL #include <chrono>
.
Einfach eine kleine Timer-Klasse, die die Anzahl an Sekunden (mit Nachkommastellen)
seit dem letzten Aufruf von reset()
wiedergibt (operator double
). Ein globales
Object namens sw::timer
wird beim Programmstart erstellt. Weil die wenigen
benötigten Instanzvariablen mutable
sind kann der Timer auch in const
Methoden
für Debugging verwendet werden. Mit sleep(seconds)
wird der Prozessthread mit
der entsprechenden Systemfunktion schlafen gelegt. Das ist schon alles :-).
Ein Anwendungsbeispiel steht in der Quelltext-Headerdokumentation.
Note: std>=C++11
: STL #include <chrono>
.
Simply a small timer class, which returns (using operator double
) the number
of seconds (with subseconds) since the last call of the reset()
method. A
global object named sw::timer
is created during the Program initialisation.
As the few instance variables are mutable
, the timer can be used in const
methods for debugging purposes. Using sleep(seconds)
the process/thread can
be held for a defined time (with subseconds again). And that's pretty much it :-)
You find the example in the header documentation of the source code.
Dateien
Files
Quelltext
Source code
/**
* @package de.atwillys.cc.swl
* @license BSD (simplified)
* @author Stefan Wilhelm (stfwi)
*
* @file timer.hh
* @ccflags
* @ldflags
* @platform linux, bsd, windows
* @standard >= c++98
*
* -----------------------------------------------------------------------------
*
* Timer class counting seconds (with sub seconds) from instantiation or last
* `reset()`. It can be used in `const` context as the instance variables are
* `mutable`.
*
* The 'standard' implementation OBJECT is sw::timer, which uses double precision
* basic_timer<double>:
*
* `const basic_timer<double> & timer = basic_timer<double>::instance();`
*
* As the (namespace global) variable referrs to a template singleton instance,
* the same timer object will be used in all source files.
*
* If you like to instnatiate a separate timer, use
*
* ::sw::basic_timer<double> my_timer;
*
* -----------------------------------------------------------------------------
*
* Simple example:
*
* #include <sw/timer.hh>
* #include <iostream>
* using namespace sw;
* using namespace std;
*
* int main(int argc, char** argv)
* {
* for(int i=0; i<3; ++i) {
* timer.sleep(0.1);
* cout << "timer=" << timer << endl;
* }
* timer_t t;
* for(int i=0; i<3; ++i) {
* timer.sleep(0.1);
* cout << "timer=" << timer << " | t=" << t << endl;
* }
* }
*
* -----------------------------------------------------------------------------
* +++ BSD license header +++
* Copyright (c) 2008-2014, Stefan Wilhelm (stfwi, <cerbero s@atwilly s.de>)
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: (1) Redistributions
* of source code must retain the above copyright notice, this list of conditions
* and the following disclaimer. (2) Redistributions in binary form must reproduce
* the above copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the distribution.
* (3) Neither the name of atwillys.de nor the names of its contributors may be
* used to endorse or promote products derived from this software without specific
* prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS
* AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
* -----------------------------------------------------------------------------
*/
#ifndef SW_TIMER_HH
#define SW_TIMER_HH
#if defined (OS_LINUX) || defined (__linux__)
#include <time.h>
#include <sys/time.h>
#ifndef OS_LINUX
#define OS_LINUX
#endif
#elif defined (OS_MAC) || defined __APPLE__ & __MACH__
#ifndef OS_MAC
#define OS_MAC
#endif
#include <sys/time.h>
#elif defined (OS_BSD) || defined (_unix) || defined (__unix) || defined (__unix__)
#ifndef OS_UNIX
#define OS_UNIX
#endif
#include <sys/time.h>
#elif defined(OS_WIN) || defined (_WINDOWS_) || defined(_WIN32) || defined(__MSC_VER)
#ifndef OS_WIN
#define OS_WIN
#endif
#include <windows.h>
#endif
namespace sw { namespace detail {
/**
* Base class template basic_timer
*/
template <typename value_type>
class basic_timer
{
public:
inline basic_timer()
{ reset(); }
virtual ~basic_timer()
{ ; }
inline void reset() const throw()
{
#ifndef OS_WIN
ts_ = now();
#else
LARGE_INTEGER f; ::QueryPerformanceFrequency(&f);
tb_ = 1.0/((value_type) f.QuadPart);
::QueryPerformanceCounter(&ts_);
#endif
}
inline value_type val() const throw ()
{
#ifndef OS_WIN
return now() - ts_;
#else
LARGE_INTEGER t; ::QueryPerformanceCounter(&t);
return tb_ * (t.QuadPart - ts_.QuadPart);
#endif
}
inline operator value_type () const throw ()
{ return val(); }
static const basic_timer<value_type> t;
protected:
#ifndef OS_WIN
inline value_type now() const throw()
{
struct timeval ts;
::gettimeofday(&ts, 0);
return (value_type)(1.e-6 * ts.tv_usec) + ts.tv_sec;
}
#endif
#ifndef OS_WIN
mutable value_type ts_;
#else
mutable value_type tb_;
mutable LARGE_INTEGER ts_;
#endif
};
template <typename value_type>
const basic_timer<value_type> basic_timer<value_type>::t;
}}
namespace sw {
typedef detail::basic_timer<double> timer;
}
#endif