From ae932b4fa980acb8c80946d098c79e2ab3611d3f Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Wed, 3 Apr 2024 00:16:04 +0800 Subject: New Row_printNanoseconds() function It prints the time in one of these formats: nanoseconds, "fraction of seconds", "minutes + seconds + milliseconds". If the total time is greater than 10 minutes, it uses Row_printTime() to print higher time units. Signed-off-by: Kang-Che Sung --- Row.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ Row.h | 3 +++ 2 files changed, 54 insertions(+) diff --git a/Row.c b/Row.c index a175c9e3..66a4aa07 100644 --- a/Row.c +++ b/Row.c @@ -403,6 +403,57 @@ void Row_printTime(RichString* str, unsigned long long totalHundredths, bool col } } +void Row_printNanoseconds(RichString* str, unsigned long long totalNanoseconds, bool coloring) { + if (totalNanoseconds == 0) { + int shadowColor = coloring ? CRT_colors[PROCESS_SHADOW] : CRT_colors[PROCESS]; + + RichString_appendAscii(str, shadowColor, " 0ns "); + return; + } + + char buffer[10]; + int len; + int baseColor = CRT_colors[PROCESS]; + + if (totalNanoseconds < 1000000) { + len = xSnprintf(buffer, sizeof(buffer), "%6luns ", (unsigned long)totalNanoseconds); + RichString_appendnAscii(str, baseColor, buffer, len); + return; + } + + unsigned long long totalMicroseconds = totalNanoseconds / 1000; + if (totalMicroseconds < 1000000) { + len = xSnprintf(buffer, sizeof(buffer), ".%06lus ", (unsigned long)totalMicroseconds); + RichString_appendnAscii(str, baseColor, buffer, len); + } + + unsigned long long totalSeconds = totalMicroseconds / 1000000; + unsigned long microseconds = totalMicroseconds % 1000000; + if (totalSeconds < 60) { + int width = 5; + unsigned long fraction = microseconds; + if (totalSeconds >= 10) { + width--; + fraction /= 10; + } + len = xSnprintf(buffer, sizeof(buffer), "%u.%0*lus ", (unsigned int)totalSeconds, width, fraction); + RichString_appendnAscii(str, baseColor, buffer, len); + return; + } + + if (totalSeconds < 600) { + unsigned int minutes = totalSeconds / 60; + unsigned int seconds = totalSeconds % 60; + unsigned int milliseconds = microseconds / 1000; + len = xSnprintf(buffer, sizeof(buffer), "%u:%02u.%03u ", minutes, seconds, milliseconds); + RichString_appendnAscii(str, baseColor, buffer, len); + return; + } + + unsigned long long totalHundredths = totalMicroseconds / 1000 / 10; + Row_printTime(str, totalHundredths, coloring); +} + void Row_printRate(RichString* str, double rate, bool coloring) { char buffer[16]; diff --git a/Row.h b/Row.h index f67c6103..6d882909 100644 --- a/Row.h +++ b/Row.h @@ -154,6 +154,9 @@ void Row_printCount(RichString* str, unsigned long long number, bool coloring); /* Takes time in hundredths of a seconds. Prints 9 columns. */ void Row_printTime(RichString* str, unsigned long long totalHundredths, bool coloring); +/* Takes time in nanoseconds. Prints 9 columns. */ +void Row_printNanoseconds(RichString* str, unsigned long long totalNanoseconds, bool coloring); + /* Takes rate in bare unit (base 1024) per second. Prints 12 columns. */ void Row_printRate(RichString* str, double rate, bool coloring); -- cgit v1.2.3