|
| 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 | +} |
0 commit comments