Skip to content

Commit 1bc8022

Browse files
committed
Tweak native formatting function (again)
1 parent e932f02 commit 1bc8022

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

runtime/commonMain/src/org/jetbrains/gradle/benchmarks/ReportBenchmarksStatistics.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class ReportBenchmarksStatistics(values: DoubleArray) {
8282

8383
expect fun Double.format(precision: Int): String
8484

85-
fun Double.formatAtMost(precision: Int): String {
85+
fun Double.formatSignificant(precision: Int): String {
8686
val d = (precision - ceil(log10(this)).toInt()).coerceAtLeast(0) // display 4 significant digits
8787
return format(d)
8888
}
@@ -91,8 +91,8 @@ fun Double.formatAtMost(precision: Int): String {
9191
fun Double.nanosToText(mode: Mode, unit: BenchmarkTimeUnit): String {
9292
val value = nanosToSample(mode, unit)
9393
return when (mode) {
94-
Mode.Throughput -> "${value.formatAtMost(6)} ops/${unit.toText()}"
95-
Mode.AverageTime -> "${value.formatAtMost(6)} ${unit.toText()}/op"
94+
Mode.Throughput -> "${value.formatSignificant(6)} ops/${unit.toText()}"
95+
Mode.AverageTime -> "${value.formatSignificant(6)} ${unit.toText()}/op"
9696
else -> throw UnsupportedOperationException("$mode is not supported")
9797
}
9898
}
@@ -108,8 +108,8 @@ fun unitText(mode: Mode, unit: BenchmarkTimeUnit) = when (mode) {
108108
fun Double.sampleToText(mode: Mode, unit: BenchmarkTimeUnit): String {
109109
val value = this
110110
return when (mode) {
111-
Mode.Throughput -> "${value.formatAtMost(6)} ops/${unit.toText()}"
112-
Mode.AverageTime -> "${value.formatAtMost(6)} ${unit.toText()}/op"
111+
Mode.Throughput -> "${value.formatSignificant(6)} ops/${unit.toText()}"
112+
Mode.AverageTime -> "${value.formatSignificant(6)} ${unit.toText()}/op"
113113
else -> throw UnsupportedOperationException("$mode is not supported")
114114
}
115115
}

runtime/commonTest/src/org/jetbrains/gradle/benchmarks/tests/FormatTests.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ import kotlin.test.*
66

77
class FormatTests {
88
@Test
9-
fun formatPrecision() {
10-
assertEquals("1,200", 1200.0.formatAtMost(4))
11-
assertEquals("12.03", 12.03.formatAtMost(4))
12-
assertEquals("1,200", 1200.00005.formatAtMost(4))
13-
assertEquals("0.00000005000", 0.00000005.formatAtMost(4))
14-
assertEquals("0.0001001", 0.00010005.formatAtMost(4))
9+
fun format() {
10+
assertEquals("1,200.0000", 1200.0.format(4))
11+
assertEquals("12.0300", 12.03.format(4))
12+
assertEquals("1,200.0001", 1200.00005.format(4))
13+
assertEquals("0.0000", 0.00000005.format(4))
14+
assertEquals("0.0001", 0.00010005.format(4))
1515
}
1616

1717
@Test
18-
fun formatTimeUnits() {
18+
fun formatSignificant() {
19+
assertEquals("1,200", 1200.0.formatSignificant(4))
20+
assertEquals("12.03", 12.03.formatSignificant(4))
21+
assertEquals("1,200", 1200.00005.formatSignificant(4))
22+
assertEquals("0.00000005000", 0.00000005.formatSignificant(4))
23+
assertEquals("0.0001001", 0.00010005.formatSignificant(4))
24+
}
25+
26+
@Test
27+
fun nanosToText() {
1928
assertEquals("83.3333 ops/us", 12.0.nanosToText(Mode.Throughput, BenchmarkTimeUnit.MICROSECONDS))
2029
assertEquals("0.0120000 us/op", 12.0.nanosToText(Mode.AverageTime, BenchmarkTimeUnit.MICROSECONDS))
2130
}

runtime/jsMain/src/org/jetbrains/gradle/benchmarks/js/JsExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class JsExecutor(name: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o
8989
.toDoubleArray()
9090
val result = ReportBenchmarksStatistics.createResult(benchmark, config, samples)
9191
val message = with(result) {
92-
" ~ ${score.sampleToText(config.mode, config.outputTimeUnit)} ±${(error / score * 100).formatAtMost(2)}%"
92+
" ~ ${score.sampleToText(config.mode, config.outputTimeUnit)} ±${(error / score * 100).formatSignificant(2)}%"
9393
}
9494
val error = event.target.error
9595
if (error == null) {

runtime/nativeMain/src/org/jetbrains/gradle/benchmarks/Utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ actual fun Double.format(precision: Int): String {
77
val longPart = toLong()
88
val fractional = this - longPart
99
val thousands = longPart.toString().replace(Regex("\\B(?=(\\d{3})+(?!\\d))"), ",")
10-
if (fractional < DBL_EPSILON || precision == 0)
10+
if (precision == 0)
1111
return thousands
1212

1313
return memScoped {

runtime/nativeMain/src/org/jetbrains/gradle/benchmarks/native/NativeExecutor.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class NativeExecutor(name: String, args: Array<out String>) : SuiteExecutor(name
4646
" ~ ${score.sampleToText(
4747
config.mode,
4848
config.outputTimeUnit
49-
)} ±${(error / score * 100).formatAtMost(2)}%"
49+
)} ±${(error / score * 100).formatSignificant(2)}%"
5050
}
5151

5252
reporter.endBenchmark(executionName, benchmark.name, BenchmarkProgress.FinishStatus.Success, message)

0 commit comments

Comments
 (0)