Common: Added Timer

This commit is contained in:
Paul Beckingham 2015-12-21 00:28:37 -05:00
parent 25a6dd4920
commit cf19677488
3 changed files with 179 additions and 0 deletions

View file

@ -6,6 +6,7 @@ include_directories (${CMAKE_SOURCE_DIR}
set (common_SRCS Color.cpp Color.h
FS.cpp FS.h
RX.cpp RX.h
Timer.cpp Timer.h
text.cpp text.h
utf8.cpp utf8.h
wcwidth6.cpp)

119
src/common/Timer.cpp Normal file
View file

@ -0,0 +1,119 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#include <cmake.h>
#include <Timer.h>
#include <iostream>
#include <iomanip>
#include <sstream>
////////////////////////////////////////////////////////////////////////////////
// Timer starts when the object is constructed.
Timer::Timer ()
: _description ("-")
, _running (false)
, _total (0)
{
}
////////////////////////////////////////////////////////////////////////////////
// Timer starts when the object is constructed with a description.
Timer::Timer (const std::string& description)
: _description (description)
, _running (false)
, _total (0)
{
start ();
}
////////////////////////////////////////////////////////////////////////////////
// Timer stops when the object is destructed.
Timer::~Timer ()
{
stop ();
std::stringstream s;
s << "Timer " // No i18n
<< _description
<< " "
<< std::setprecision (6)
#ifndef HAIKU
// Haiku fails on this - don't know why.
<< std::fixed
#endif
<< _total / 1000000.0
<< " sec\n";
std::cerr << s.str ();
}
////////////////////////////////////////////////////////////////////////////////
void Timer::start ()
{
if (!_running)
{
gettimeofday (&_start, NULL);
_running = true;
}
}
////////////////////////////////////////////////////////////////////////////////
void Timer::stop ()
{
if (_running)
{
struct timeval end;
gettimeofday (&end, NULL);
_running = false;
_total += (end.tv_sec - _start.tv_sec) * 1000000
+ (end.tv_usec - _start.tv_usec);
}
}
////////////////////////////////////////////////////////////////////////////////
unsigned long Timer::total () const
{
return _total;
}
////////////////////////////////////////////////////////////////////////////////
void Timer::subtract (unsigned long value)
{
if (value > _total)
_total = 0;
else
_total -= value;
}
////////////////////////////////////////////////////////////////////////////////
unsigned long Timer::now ()
{
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_sec * 1000000 + now.tv_usec;
}
////////////////////////////////////////////////////////////////////////////////

59
src/common/Timer.h Normal file
View file

@ -0,0 +1,59 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2006 - 2015, Paul Beckingham, Federico Hernandez.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// http://www.opensource.org/licenses/mit-license.php
//
////////////////////////////////////////////////////////////////////////////////
#ifndef INCLUDED_TIMER
#define INCLUDED_TIMER
#include <string>
#include <sys/time.h>
// Timer is a scope-activated timer that dumps to std::cout at end of scope.
class Timer
{
public:
Timer ();
Timer (const std::string&);
~Timer ();
Timer (const Timer&);
Timer& operator= (const Timer&);
void start ();
void stop ();
unsigned long total () const;
void subtract (unsigned long);
static unsigned long now ();
private:
std::string _description;
bool _running;
struct timeval _start;
unsigned long _total;
};
#endif
////////////////////////////////////////////////////////////////////////////////