From 4d76ff96811ea45ef4fba9d6892941f3aafd9614 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Sun, 28 Oct 2018 15:04:42 -0700 Subject: [PATCH] [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. --- benchmark/CMakeLists.txt | 1 + benchmark/single-source/FlattenList.swift | 61 +++++++++++++++++++++++ benchmark/utils/main.swift | 3 ++ 3 files changed, 65 insertions(+) create mode 100644 benchmark/single-source/FlattenList.swift diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt index bd6280dc8995a..62d8aa2473c9e 100644 --- a/benchmark/CMakeLists.txt +++ b/benchmark/CMakeLists.txt @@ -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 diff --git a/benchmark/single-source/FlattenList.swift b/benchmark/single-source/FlattenList.swift new file mode 100644 index 0000000000000..e84092f3ef71a --- /dev/null +++ b/benchmark/single-source/FlattenList.swift @@ -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)) + } +} diff --git a/benchmark/utils/main.swift b/benchmark/utils/main.swift index cac9c2f07b1a5..cf0214353d843 100644 --- a/benchmark/utils/main.swift +++ b/benchmark/utils/main.swift @@ -72,6 +72,7 @@ import ErrorHandling import Exclusivity import ExistentialPerformance import Fibonacci +import FlattenList import FloatingPointPrinting import Hanoi import Hash @@ -239,6 +240,8 @@ registerBenchmark(ErrorHandling) registerBenchmark(Exclusivity) registerBenchmark(ExistentialPerformance) registerBenchmark(Fibonacci) +registerBenchmark(FlattenListLoop) +registerBenchmark(FlattenListFlatMap) registerBenchmark(FloatingPointPrinting) registerBenchmark(Hanoi) registerBenchmark(HashTest)