+
+/**
+ * Convert a CPU / GPU time to a textual representation.
+ * This includes automatic unit selection.
+ */
+QString getTimeString(int64_t time, int64_t unitTime)
+{
+ QString text;
+ QString unit = " ns";
+ double unitScale = 1;
+
+ if (unitTime == 0) {
+ unitTime = time;
+ }
+
+ if (unitTime >= 60e9) {
+ int64_t mins = time / 60e9;
+ text += QString("%1 m ").arg(mins);
+
+ time -= mins * 60e9;
+ unit = " s";
+ unitScale = 1e9;
+ } else if (unitTime >= 1e9) {
+ unit = " s";
+ unitScale = 1e9;
+ } else if (unitTime >= 1e6) {
+ unit = " ms";
+ unitScale = 1e6;
+ } else if (unitTime >= 1e3) {
+ unit = " us";
+ unitScale = 1e3;
+ }
+
+ /* 3 decimal places */
+ text += QString("%1").arg(time / unitScale, 0, 'f', 3);
+
+ /* Remove trailing 0 */
+ while(text.endsWith('0'))
+ text.truncate(text.length() - 1);
+
+ /* Remove trailing decimal point */
+ if (text.endsWith(QLocale::system().decimalPoint()))
+ text.truncate(text.length() - 1);
+
+ return text + unit;
+}
+
+