Skip to content

Commit 45ee2f5

Browse files
authored
Make numbers more compact in the console output (#61)
This adds a few minor changes to reduce column width of numeric output with console reporter: * Floating point numbers that end with `.0`, only show the integer part * Non-integer floating point numbers round off to 3 digits after the dot
1 parent 6837afa commit 45ee2f5

File tree

3 files changed

+35
-32
lines changed

3 files changed

+35
-32
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ OPTIONS:
5050
-h, --help Show help information.
5151
5252
$ swift run -c release BenchmarkMinimalExample
53-
running add string no capacity... done! (1795.09 ms)
54-
running add string reserved capacity... done! (1815.80 ms)
53+
running add string no capacity... done! (1832.52 ms)
54+
running add string reserved capacity... done! (1813.96 ms)
5555
56-
name time std iterations
57-
-------------------------------------------------------------------------
58-
add string no capacity 35380.0 ns ± 5035.122635576142 38603
59-
add string reserved capacity 37466.0 ns ± 4940.675061813501 36990
56+
name time std iterations
57+
-----------------------------------------------------------
58+
add string no capacity 37435 ns ± 6.22 % 37196
59+
add string reserved capacity 37022 ns ± 1.75 % 37749
6060
```
6161

6262
For more examples, see

Sources/Benchmark/BenchmarkFormatter.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ public enum BenchmarkFormatter {
2222
if string.hasSuffix(".0") {
2323
return String(string.dropLast(2))
2424
} else {
25-
return string
25+
return String(format: "%.3f", value)
2626
}
2727
}
2828

2929
/// Show number with the corresponding time unit.
3030
public static let time: Formatter = { (value, settings) in
31-
return "\(value) \(settings.timeUnit)"
31+
let num = number(value, settings)
32+
return "\(num) \(settings.timeUnit)"
3233
}
3334

3435
/// Show number with the corresponding inverse time unit.
3536
public static let inverseTime: Formatter = { (value, settings) in
36-
return "\(value) /\(settings.inverseTimeUnit)"
37+
let num = number(value, settings)
38+
return "\(num) /\(settings.inverseTimeUnit)"
3739
}
3840

3941
/// Show value as percentage.
@@ -43,7 +45,8 @@ public enum BenchmarkFormatter {
4345

4446
/// Show value as plus or minus standard deviation.
4547
public static let std: Formatter = { (value, settings) in
46-
return "± " + String(value)
48+
let num = number(value, settings)
49+
return "± \(num)"
4750
}
4851

4952
/// Show value as plus or minus standard deviation in percentage.

Tests/BenchmarkTests/BenchmarkReporterTests.swift

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ final class BenchmarkReporterTests: XCTestCase {
6767
counters: [:]),
6868
]
6969
let expected = #"""
70-
name time std iterations
71-
-----------------------------------------------
72-
MySuite.fast 1500.0 ns ± 47.14 % 2
73-
MySuite.slow 1500000.0 ns ± 47.14 % 2
70+
name time std iterations
71+
---------------------------------------------
72+
MySuite.fast 1500 ns ± 47.14 % 2
73+
MySuite.slow 1500000 ns ± 47.14 % 2
7474
"""#
7575
assertConsoleReported(results, expected)
7676
}
@@ -91,10 +91,10 @@ final class BenchmarkReporterTests: XCTestCase {
9191
counters: [:]),
9292
]
9393
let expected = #"""
94-
name time std iterations foo
95-
---------------------------------------------------
96-
MySuite.fast 1500.0 ns ± 47.14 % 2 7
97-
MySuite.slow 1500000.0 ns ± 47.14 % 2 0
94+
name time std iterations foo
95+
-------------------------------------------------
96+
MySuite.fast 1500 ns ± 47.14 % 2 7
97+
MySuite.slow 1500000 ns ± 47.14 % 2 0
9898
"""#
9999
assertConsoleReported(results, expected)
100100
}
@@ -115,10 +115,10 @@ final class BenchmarkReporterTests: XCTestCase {
115115
counters: [:]),
116116
]
117117
let expected = #"""
118-
name time std iterations warmup
119-
-------------------------------------------------------
120-
MySuite.fast 1500.0 ns ± 47.14 % 2 60.0 ns
121-
MySuite.slow 1500000.0 ns ± 47.14 % 2 0.0 ns
118+
name time std iterations warmup
119+
----------------------------------------------------
120+
MySuite.fast 1500 ns ± 47.14 % 2 60 ns
121+
MySuite.slow 1500000 ns ± 47.14 % 2 0 ns
122122
"""#
123123
assertConsoleReported(results, expected)
124124
}
@@ -151,12 +151,12 @@ final class BenchmarkReporterTests: XCTestCase {
151151
counters: [:]),
152152
]
153153
let expected = #"""
154-
name time std iterations
155-
-----------------------------------------------
156-
MySuite.ns 123456789.0 ns ± 0.00 % 1
157-
MySuite.us 123456.789 us ± 0.00 % 1
158-
MySuite.ms 123.456789 ms ± 0.00 % 1
159-
MySuite.s 0.123456789 s ± 0.00 % 1
154+
name time std iterations
155+
----------------------------------------------
156+
MySuite.ns 123456789 ns ± 0.00 % 1
157+
MySuite.us 123456.789 us ± 0.00 % 1
158+
MySuite.ms 123.457 ms ± 0.00 % 1
159+
MySuite.s 0.123 s ± 0.00 % 1
160160
"""#
161161
assertConsoleReported(results, expected)
162162
}
@@ -177,10 +177,10 @@ final class BenchmarkReporterTests: XCTestCase {
177177
counters: [:]),
178178
]
179179
let expected = #"""
180-
name min max
181-
-----------------------------------
182-
MySuite.fast 1000.0 ns
183-
MySuite.slow 2000000.0 ns
180+
name min max
181+
-------------------------------
182+
MySuite.fast 1000 ns
183+
MySuite.slow 2000000 ns
184184
"""#
185185
assertConsoleReported(results, expected)
186186
}

0 commit comments

Comments
 (0)