Skip to content

[benchmark] Add two benchmarks that show performance of flattening an… #20116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ set(SWIFT_BENCH_MODULES
single-source/Exclusivity
single-source/ExistentialPerformance
single-source/Fibonacci
single-source/FlattenList
single-source/FloatingPointPrinting
single-source/Hanoi
single-source/Hash
Expand Down
61 changes: 61 additions & 0 deletions benchmark/single-source/FlattenList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//===--- FlattenList.swift ------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils

public let FlattenListLoop = BenchmarkInfo(
name: "FlattenListLoop",
runFunction: run_FlattenListLoop,
tags: [.api, .validation],
setUpFunction: { blackHole(inputArray) })

public let FlattenListFlatMap = BenchmarkInfo(
name: "FlattenListFlatMap",
runFunction: run_FlattenListFlatMap,
tags: [.api, .validation],
setUpFunction: { blackHole(inputArray) })

let inputArray: [(Int, Int, Int, Int)] = (0..<(1<<16)).map { _ in
(5, 6, 7, 8)
}

func flattenFlatMap(_ input: [(Int, Int, Int, Int)]) -> [Int] {
return input.flatMap { [$0.0, $0.1, $0.2, $0.3] }
}

func flattenLoop(_ input: [(Int, Int, Int, Int)]) -> [Int] {
var flattened: [Int] = []
flattened.reserveCapacity(input.count * 4)

for (x, y, z, w) in input {
flattened.append(x)
flattened.append(y)
flattened.append(z)
flattened.append(w)
}

return flattened
}

@inline(never)
public func run_FlattenListLoop(_ N: Int) {
for _ in 0..<5*N {
blackHole(flattenLoop(inputArray))
}
}

@inline(never)
public func run_FlattenListFlatMap(_ N: Int) {
for _ in 1...5*N {
blackHole(flattenFlatMap(inputArray))
}
}
3 changes: 3 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import ErrorHandling
import Exclusivity
import ExistentialPerformance
import Fibonacci
import FlattenList
import FloatingPointPrinting
import Hanoi
import Hash
Expand Down Expand Up @@ -239,6 +240,8 @@ registerBenchmark(ErrorHandling)
registerBenchmark(Exclusivity)
registerBenchmark(ExistentialPerformance)
registerBenchmark(Fibonacci)
registerBenchmark(FlattenListLoop)
registerBenchmark(FlattenListFlatMap)
registerBenchmark(FloatingPointPrinting)
registerBenchmark(Hanoi)
registerBenchmark(HashTest)
Expand Down