Skip to content

Commit d8adfc7

Browse files
committed
[benchmark] MapReduceClass2 and NSDecimalNumber
Since this benchmark has been significantly modified and needs to be renamed, we can also lower the workload by a factor of 10, to keep up with the best practices. The old benchmark that uses `NSDecimalNumber` as the tested class is renamed to `MapReduceNSDecimalNumber` and the renamed `MapReduceClass2` now newly measures Swift class `Box` that wrap an `Int`. Short versions were modified analogously.
1 parent 96ff53d commit d8adfc7

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

benchmark/single-source/MapReduce.swift

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ public let MapReduce = [
1818
BenchmarkInfo(name: "MapReduceAnyCollection", runFunction: run_MapReduceAnyCollection, tags: [.validation, .algorithm]),
1919
BenchmarkInfo(name: "MapReduceAnyCollectionShort", runFunction: run_MapReduceAnyCollectionShort, tags: [.validation, .algorithm]),
2020
BenchmarkInfo(name: "MapReduceClass2", runFunction: run_MapReduceClass, tags: [.validation, .algorithm],
21-
setUpFunction: { decimals(1000) }, tearDownFunction: releaseDecimals),
21+
setUpFunction: { boxedNumbers(1000) }, tearDownFunction: releaseDecimals),
2222
BenchmarkInfo(name: "MapReduceClassShort2", runFunction: run_MapReduceClassShort, tags: [.validation, .algorithm],
23+
setUpFunction: { boxedNumbers(10) }, tearDownFunction: releaseDecimals),
24+
BenchmarkInfo(name: "MapReduceNSDecimalNumber", runFunction: run_MapReduceNSDecimalNumber, tags: [.validation, .algorithm],
25+
setUpFunction: { decimals(1000) }, tearDownFunction: releaseDecimals),
26+
BenchmarkInfo(name: "MapReduceNSDecimalNumberShort", runFunction: run_MapReduceNSDecimalNumberShort, tags: [.validation, .algorithm],
2327
setUpFunction: { decimals(10) }, tearDownFunction: releaseDecimals),
2428
BenchmarkInfo(name: "MapReduceLazyCollection", runFunction: run_MapReduceLazyCollection, tags: [.validation, .algorithm]),
2529
BenchmarkInfo(name: "MapReduceLazyCollectionShort", runFunction: run_MapReduceLazyCollectionShort, tags: [.validation, .algorithm]),
@@ -41,6 +45,15 @@ func decimals(_ n: Int) {}
4145
func releaseDecimals() {}
4246
#endif
4347

48+
class Box {
49+
var v: Int
50+
init(_ v: Int) { self.v = v }
51+
}
52+
53+
var boxedNumbers : [Box]!
54+
func boxedNumbers(_ n: Int) { boxedNumbers = (0..<n).map { Box($0) } }
55+
func releaseboxedNumbers() { boxedNumbers = nil }
56+
4457
@inline(never)
4558
public func run_MapReduce(_ N: Int) {
4659
var numbers = [Int](0..<1000)
@@ -160,12 +173,12 @@ public func run_MapReduceShortString(_ N: Int) {
160173
}
161174

162175
@inline(never)
163-
public func run_MapReduceClass(_ N: Int) {
176+
public func run_MapReduceNSDecimalNumber(_ N: Int) {
164177
#if _runtime(_ObjC)
165178
let numbers: [NSDecimalNumber] = decimals
166179

167180
var c = 0
168-
for _ in 1...N*100 {
181+
for _ in 1...N*10 {
169182
let mapped = numbers.map { $0.intValue &+ 5 }
170183
c += mapped.reduce(0, &+)
171184
}
@@ -174,15 +187,40 @@ public func run_MapReduceClass(_ N: Int) {
174187
}
175188

176189
@inline(never)
177-
public func run_MapReduceClassShort(_ N: Int) {
190+
public func run_MapReduceNSDecimalNumberShort(_ N: Int) {
178191
#if _runtime(_ObjC)
179192
let numbers: [NSDecimalNumber] = decimals
180193

181194
var c = 0
182-
for _ in 1...N*10000 {
195+
for _ in 1...N*1_000 {
183196
let mapped = numbers.map { $0.intValue &+ 5 }
184197
c += mapped.reduce(0, &+)
185198
}
186199
CheckResults(c != 0)
187200
#endif
188201
}
202+
203+
204+
@inline(never)
205+
public func run_MapReduceClass(_ N: Int) {
206+
let numbers: [Box] = boxedNumbers
207+
208+
var c = 0
209+
for _ in 1...N*10 {
210+
let mapped = numbers.map { $0.v &+ 5 }
211+
c += mapped.reduce(0, &+)
212+
}
213+
CheckResults(c != 0)
214+
}
215+
216+
@inline(never)
217+
public func run_MapReduceClassShort(_ N: Int) {
218+
let numbers: [Box] = boxedNumbers
219+
220+
var c = 0
221+
for _ in 1...N*1_000 {
222+
let mapped = numbers.map { $0.v &+ 5 }
223+
c += mapped.reduce(0, &+)
224+
}
225+
CheckResults(c != 0)
226+
}

0 commit comments

Comments
 (0)