Skip to content

Commit

Permalink
[benchmark] Extract setup: MapReduceClass(Small)
Browse files Browse the repository at this point in the history
MapReduceClass had setup overhead fo 868 μs (7%).

Setup overhead of MapReduceClassShort was practically lost in the measurement noise from it’s artificially high base load, but it was there.

Extracting the decimal array initialization into `SetUpFunction` also takes out the cost of releasing the [NSDecimalNumber], which turns out to be about half of the measured runtime in the case of MapReduceClass benchmark. This significantly changes the reported runtimes (to about half), therfore the modified benchmarks get a new name with suffix `2`.
  • Loading branch information
palimondo committed Oct 24, 2018
1 parent b55244d commit 96ff53d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions benchmark/single-source/MapReduce.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ public let MapReduce = [
BenchmarkInfo(name: "MapReduce", runFunction: run_MapReduce, tags: [.validation, .algorithm]),
BenchmarkInfo(name: "MapReduceAnyCollection", runFunction: run_MapReduceAnyCollection, tags: [.validation, .algorithm]),
BenchmarkInfo(name: "MapReduceAnyCollectionShort", runFunction: run_MapReduceAnyCollectionShort, tags: [.validation, .algorithm]),
BenchmarkInfo(name: "MapReduceClass", runFunction: run_MapReduceClass, tags: [.validation, .algorithm]),
BenchmarkInfo(name: "MapReduceClassShort", runFunction: run_MapReduceClassShort, tags: [.validation, .algorithm]),
BenchmarkInfo(name: "MapReduceClass2", runFunction: run_MapReduceClass, tags: [.validation, .algorithm],
setUpFunction: { decimals(1000) }, tearDownFunction: releaseDecimals),
BenchmarkInfo(name: "MapReduceClassShort2", runFunction: run_MapReduceClassShort, tags: [.validation, .algorithm],
setUpFunction: { decimals(10) }, tearDownFunction: releaseDecimals),
BenchmarkInfo(name: "MapReduceLazyCollection", runFunction: run_MapReduceLazyCollection, tags: [.validation, .algorithm]),
BenchmarkInfo(name: "MapReduceLazyCollectionShort", runFunction: run_MapReduceLazyCollectionShort, tags: [.validation, .algorithm]),
BenchmarkInfo(name: "MapReduceLazySequence", runFunction: run_MapReduceLazySequence, tags: [.validation, .algorithm]),
Expand All @@ -28,6 +30,17 @@ public let MapReduce = [
BenchmarkInfo(name: "MapReduceString", runFunction: run_MapReduceString, tags: [.validation, .algorithm, .String]),
]

#if _runtime(_ObjC)
var decimals : [NSDecimalNumber]!
func decimals(_ n: Int) {
decimals = (0..<n).map { NSDecimalNumber(value: $0) }
}
func releaseDecimals() { decimals = nil }
#else
func decimals(_ n: Int) {}
func releaseDecimals() {}
#endif

@inline(never)
public func run_MapReduce(_ N: Int) {
var numbers = [Int](0..<1000)
Expand Down Expand Up @@ -149,7 +162,7 @@ public func run_MapReduceShortString(_ N: Int) {
@inline(never)
public func run_MapReduceClass(_ N: Int) {
#if _runtime(_ObjC)
let numbers = (0..<1000).map { NSDecimalNumber(value: $0) }
let numbers: [NSDecimalNumber] = decimals

var c = 0
for _ in 1...N*100 {
Expand All @@ -163,7 +176,7 @@ public func run_MapReduceClass(_ N: Int) {
@inline(never)
public func run_MapReduceClassShort(_ N: Int) {
#if _runtime(_ObjC)
let numbers = (0..<10).map { NSDecimalNumber(value: $0) }
let numbers: [NSDecimalNumber] = decimals

var c = 0
for _ in 1...N*10000 {
Expand All @@ -173,4 +186,3 @@ public func run_MapReduceClassShort(_ N: Int) {
CheckResults(c != 0)
#endif
}

0 comments on commit 96ff53d

Please sign in to comment.