Skip to content

Commit 27aba5a

Browse files
committed
Rework humanTimeUnit() formats of meters
The new time formats will print at most 6 characters (increased from 5) Signed-off-by: Kang-Che Sung <[email protected]>
1 parent 3c32af7 commit 27aba5a

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

GPUMeter.c

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,64 @@ bool GPUMeter_active(void) {
3535
return activeMeters > 0;
3636
}
3737

38-
static int humanTimeUnit(char* buffer, size_t size, unsigned long long int value) {
38+
static int humanTimeUnit(char* buffer, size_t size, unsigned long long totalNanoseconds) {
39+
if (totalNanoseconds < 10000)
40+
return xSnprintf(buffer, size, "%4uns", (unsigned int)totalNanoseconds);
3941

40-
if (value < 1000)
41-
return xSnprintf(buffer, size, "%3lluns", value);
42-
43-
if (value < 10000)
44-
return xSnprintf(buffer, size, "%1llu.%1lluus", value / 1000, (value % 1000) / 100);
45-
46-
value /= 1000;
42+
unsigned long long value = totalNanoseconds / 100;
4743

4844
if (value < 1000)
49-
return xSnprintf(buffer, size, "%3lluus", value);
45+
return xSnprintf(buffer, size, "%u.%uus", (unsigned int)(value / 10), (unsigned int)(value % 10));
46+
47+
value /= 10; // microseconds
5048

5149
if (value < 10000)
52-
return xSnprintf(buffer, size, "%1llu.%1llums", value / 1000, (value % 1000) / 100);
50+
return xSnprintf(buffer, size, "%4uus", (unsigned int)value);
51+
52+
value /= 100;
53+
54+
unsigned long long totalSeconds = value / 10000;
55+
if (totalSeconds < 60) {
56+
int width = 4;
57+
unsigned int seconds = (unsigned int)totalSeconds;
58+
unsigned int fraction = (unsigned int)(value % 10000);
59+
for (unsigned int limit = 1; seconds >= limit; limit *= 10) {
60+
width--;
61+
fraction /= 10;
62+
}
63+
// "%.u" prints no digits if (seconds == 0).
64+
return xSnprintf(buffer, size, "%.u.%0*us", seconds, width, fraction);
65+
}
5366

54-
value /= 1000;
67+
value = totalSeconds;
5568

56-
if (value < 1000)
57-
return xSnprintf(buffer, size, "%3llums", value);
69+
if (value < 3600)
70+
return xSnprintf(buffer, size, "%2um%02us", (unsigned int)value / 60, (unsigned int)value % 60);
5871

59-
if (value < 10000)
60-
return xSnprintf(buffer, size, "%1llu.%1llus", value / 1000, (value % 1000) / 100);
72+
value /= 60; // minutes
73+
74+
if (value < 1440)
75+
return xSnprintf(buffer, size, "%2uh%02um", (unsigned int)value / 60, (unsigned int)value % 60);
6176

62-
value /= 1000;
77+
value /= 60; // hours
6378

64-
if (value < 600)
65-
return xSnprintf(buffer, size, "%3llus", value);
79+
if (value < 2400)
80+
return xSnprintf(buffer, size, "%2ud%02uh", (unsigned int)value / 24, (unsigned int)value % 24);
6681

67-
value /= 60;
82+
value /= 24; // days
6883

69-
if (value < 600)
70-
return xSnprintf(buffer, size, "%3llum", value);
84+
if (value < 365)
85+
return xSnprintf(buffer, size, "%5ud", (unsigned int)value);
7186

72-
value /= 60;
87+
if (value < 3650)
88+
return xSnprintf(buffer, size, "%uy%03ud", (unsigned int)(value / 365), (unsigned int)(value % 365));
7389

74-
if (value < 96)
75-
return xSnprintf(buffer, size, "%3lluh", value);
90+
value /= 365; // years (ignore leap years)
7691

77-
value /= 24;
92+
if (value < 100000)
93+
return xSnprintf(buffer, size, "%5luy", (unsigned long)value);
7894

79-
return xSnprintf(buffer, size, "%3llud", value);
95+
return xSnprintf(buffer, size, " inf.");
8096
}
8197

8298
static void GPUMeter_updateValues(Meter* this) {

0 commit comments

Comments
 (0)