Added function SecondsToFriendlyStr. Printing playback duration.

This commit is contained in:
Adam Sawicki 2018-08-13 13:30:04 +02:00
parent 652ac27547
commit 1016cc682c
3 changed files with 78 additions and 0 deletions

View File

@ -558,3 +558,70 @@ void PrintErrorF(const wchar_t* format, ...)
va_end(argList);
}
*/
void SecondsToFriendlyStr(float seconds, std::string& out)
{
if(seconds == 0.f)
{
out = "0";
return;
}
if (seconds < 0.f)
{
out = "-";
seconds = -seconds;
}
else
{
out.clear();
}
char s[32];
// #.# ns
if(seconds < 1e-6)
{
sprintf_s(s, "%.4f ns", seconds * 1e-9);
out += s;
}
// #.# us
else if(seconds < 1e-3)
{
sprintf_s(s, "%.4f us", seconds * 1e-6);
out += s;
}
// #.# ms
else if(seconds < 1.f)
{
sprintf_s(s, "%.4f ms", seconds * 1e-3);
out += s;
}
else if(seconds < 60.f)
{
sprintf_s(s, "%.4f s", seconds);
out += s;
}
else
{
uint64_t seconds_u = (uint64_t)seconds;
// "#:## min"
if (seconds_u < 3600)
{
uint64_t minutes = seconds_u / 60;
seconds_u -= minutes * 60;
sprintf_s(s, "%llu:%02llu min", minutes, seconds_u);
out += s;
}
// "#:##:## h"
else
{
uint64_t minutes = seconds_u / 60;
seconds_u -= minutes * 60;
uint64_t hours = minutes / 60;
minutes -= hours * 60;
sprintf_s(s, "%llu:%02llu:%02llu h", hours, minutes, seconds_u);
out += s;
}
}
}

View File

@ -27,6 +27,8 @@ inline float ToFloatSeconds(duration d)
return std::chrono::duration_cast<std::chrono::duration<float>>(d).count();
}
void SecondsToFriendlyStr(float seconds, std::string& out);
template <typename T>
inline T ceil_div(T x, T y)
{

View File

@ -1067,15 +1067,24 @@ static int ProcessFile(const char* data, size_t numBytes)
{
printf("Playing...\n");
}
const time_point timeBeg = std::chrono::high_resolution_clock::now();
while(lineSplit.GetNextLine(line))
{
player.ExecuteLine(lineSplit.GetNextLineIndex(), line);
}
const duration playDuration = std::chrono::high_resolution_clock::now() - timeBeg;
// End stats.
if(g_Verbosity > VERBOSITY::MINIMUM)
{
std::string playDurationStr;
SecondsToFriendlyStr(ToFloatSeconds(playDuration), playDurationStr);
printf("Done.\n");
printf("Playback took: %s\n", playDurationStr.c_str());
printf("File lines: %zu\n", lineSplit.GetNextLineIndex());
}
}