1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
#include "stopwatch.h"
#include <cmath>
#include <sstream>
#include <boost/date_time/posix_time/posix_time.hpp>
Stopwatch::Stopwatch() : _running(false), _sum(boost::posix_time::seconds(0))
{
}
Stopwatch::Stopwatch(bool start) :
_running(start),
_startTime(boost::posix_time::microsec_clock::local_time()),
_sum(boost::posix_time::seconds(0))
{
}
Stopwatch::~Stopwatch()
{
}
void Stopwatch::Start()
{
if(!_running) {
_startTime = boost::posix_time::microsec_clock::local_time();
_running = true;
}
}
void Stopwatch::Pause()
{
if(_running) {
_sum += (boost::posix_time::microsec_clock::local_time() - _startTime);
_running = false;
}
}
void Stopwatch::Reset()
{
_running = false;
_sum = boost::posix_time::seconds(0);
}
std::string Stopwatch::ToString() const
{
if(_running) {
boost::posix_time::time_duration current = _sum + (boost::posix_time::microsec_clock::local_time() - _startTime);
return to_simple_string(current);
} else {
return to_simple_string(_sum);
}
}
std::string Stopwatch::ToShortString() const
{
const long double seconds = Seconds();
if(seconds >= 60*60*24)
return ToDaysString();
else if(seconds >= 60*60)
return ToHoursString();
else if(seconds >= 60)
return ToMinutesString();
else if(seconds >= 1.0)
return ToSecondsString();
else if(seconds >= 0.001)
return ToMilliSecondsString();
else if(seconds >= 0.000001)
return ToMicroSecondsString();
else
return ToNanoSecondsString();
}
std::string Stopwatch::ToDaysString() const
{
const long double days = roundl(Seconds() / (60.0*60.0))/24.0;
std::stringstream str;
if(days >= 10.0)
str << roundl(days) << " days";
else
str << floorl(days) << 'd' << (days*24.0) << 'h';
return str.str();
}
std::string Stopwatch::ToHoursString() const
{
const long double hours = roundl(Seconds() / 60.0)/60.0;
std::stringstream str;
if(hours >= 10.0)
str << roundl(hours) << 'h';
else
str << floorl(hours) << 'h' << (hours*60.0) << 'm';
return str.str();
}
std::string Stopwatch::ToMinutesString() const
{
const long double mins = roundl(Seconds())/60.0;
std::stringstream str;
if(mins >= 10.0)
str << roundl(mins) << " min";
else
str << floorl(mins) << 'm' << fmod(mins*60.0,60.0) << 's';
return str.str();
}
std::string Stopwatch::ToSecondsString() const
{
const long double seconds = roundl(Seconds()*10.0)/10.0;
std::stringstream str;
if(seconds >= 10.0)
str << roundl(Seconds()) << 's';
else
str << seconds << 's';
return str.str();
}
std::string Stopwatch::ToMilliSecondsString() const
{
const long double msec = roundl(Seconds()*10000.0)/10.0;
std::stringstream str;
if(msec >= 10.0)
str << roundl(Seconds()*1000.0) << "ms";
else
str << msec << "ms";
return str.str();
}
std::string Stopwatch::ToMicroSecondsString() const
{
const long double usec = roundl(Seconds()*10000000.0)/10.0;
std::stringstream str;
if(usec >= 10.0)
str << roundl(Seconds()*1000000.0) << "µs";
else
str << usec << "µs";
return str.str();
}
std::string Stopwatch::ToNanoSecondsString() const
{
const long double nsec = roundl(Seconds()*10000000000.0)/10.0;
std::stringstream str;
if(nsec >= 10.0)
str << roundl(Seconds()*1000000000.0) << "ns";
else
str << nsec << "ns";
return str.str();
}
long double Stopwatch::Seconds() const
{
if(_running) {
boost::posix_time::time_duration current = _sum + (boost::posix_time::microsec_clock::local_time() - _startTime);
return (long double) current.total_microseconds()/1000000.0;
} else {
return (long double) _sum.total_microseconds()/1000000.0;
}
}
|