PROF_COUNTER: Make Show more generic

Make the Show method accept any stream, default is current behaviour (stderr).
Can be used, therefore, also for stdout or even stringsteam.

Print a suitable timescale depending on the time recorded, from ns to s.
This commit is contained in:
John Beard 2019-06-12 13:46:55 +01:00
parent 860198edc5
commit fac3e2d662
1 changed files with 27 additions and 3 deletions

View File

@ -89,11 +89,35 @@ public:
} }
/** /**
* Print the elapsed time (in ms) to STDERR. * Print the elapsed time (in a suitable unit) to a stream.
*
* The unit is automatically chosen from ns, us, ms and s, depending on the
* size of the current count.
*
* @param the stream to print to.
*/ */
void Show() void Show( std::ostream& aStream = std::cerr )
{ {
std::cerr << m_name << " took " << msecs() << "ms." << std::endl; using DURATION = std::chrono::duration<double, std::nano>;
const auto duration = SinceStart<DURATION>();
const double cnt = duration.count();
if( m_name.size() )
{
aStream << m_name << " took ";
}
if( cnt < 1e3 )
aStream << cnt << "ns";
else if( cnt < 1e6 )
aStream << cnt / 1e3 << "µs";
else if( cnt < 1e9 )
aStream << cnt / 1e6 << "ms";
else
aStream << cnt / 1e9 << "s";
aStream << std::endl;
} }
/** /**