Skip to content

Commit 4d76ff9

Browse files
committed
[benchmark] Add two benchmarks that show performance of flattening an array.
The first is a naive imperative approach using appends in a loop. The second uses flatMap. We would like both of these to have equivalent performance.
1 parent a05dd3e commit 4d76ff9

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(SWIFT_BENCH_MODULES
8484
single-source/Exclusivity
8585
single-source/ExistentialPerformance
8686
single-source/Fibonacci
87+
single-source/FlattenList
8788
single-source/FloatingPointPrinting
8889
single-source/Hanoi
8990
single-source/Hash
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--- FlattenList.swift ------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
public let FlattenListLoop = BenchmarkInfo(
16+
name: "FlattenListLoop",
17+
runFunction: run_FlattenListLoop,
18+
tags: [.api, .validation],
19+
setUpFunction: { blackHole(inputArray) })
20+
21+
public let FlattenListFlatMap = BenchmarkInfo(
22+
name: "FlattenListFlatMap",
23+
runFunction: run_FlattenListFlatMap,
24+
tags: [.api, .validation],
25+
setUpFunction: { blackHole(inputArray) })
26+
27+
let inputArray: [(Int, Int, Int, Int)] = (0..<(1<<16)).map { _ in
28+
(5, 6, 7, 8)
29+
}
30+
31+
func flattenFlatMap(_ input: [(Int, Int, Int, Int)]) -> [Int] {
32+
return input.flatMap { [$0.0, $0.1, $0.2, $0.3] }
33+
}
34+
35+
func flattenLoop(_ input: [(Int, Int, Int, Int)]) -> [Int] {
36+
var flattened: [Int] = []
37+
flattened.reserveCapacity(input.count * 4)
38+
39+
for (x, y, z, w) in input {
40+
flattened.append(x)
41+
flattened.append(y)
42+
flattened.append(z)
43+
flattened.append(w)
44+
}
45+
46+
return flattened
47+
}
48+
49+
@inline(never)
50+
public func run_FlattenListLoop(_ N: Int) {
51+
for _ in 0..<5*N {
52+
blackHole(flattenLoop(inputArray))
53+
}
54+
}
55+
56+
@inline(never)
57+
public func run_FlattenListFlatMap(_ N: Int) {
58+
for _ in 1...5*N {
59+
blackHole(flattenFlatMap(inputArray))
60+
}
61+
}

benchmark/utils/main.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import ErrorHandling
7272
import Exclusivity
7373
import ExistentialPerformance
7474
import Fibonacci
75+
import FlattenList
7576
import FloatingPointPrinting
7677
import Hanoi
7778
import Hash
@@ -239,6 +240,8 @@ registerBenchmark(ErrorHandling)
239240
registerBenchmark(Exclusivity)
240241
registerBenchmark(ExistentialPerformance)
241242
registerBenchmark(Fibonacci)
243+
registerBenchmark(FlattenListLoop)
244+
registerBenchmark(FlattenListFlatMap)
242245
registerBenchmark(FloatingPointPrinting)
243246
registerBenchmark(Hanoi)
244247
registerBenchmark(HashTest)

0 commit comments

Comments
 (0)