blob: 58898b5df44dd6c09f86cf06e8fc106c819b1dfa [file] [log] [blame]
//===--- ReversedCollections.swift ----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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 ReversedCollections = [
BenchmarkInfo(name: "ReversedArray", runFunction: run_ReversedArray, tags: [.validation, .api, .Array]),
BenchmarkInfo(name: "ReversedBidirectional", runFunction: run_ReversedBidirectional, tags: [.validation, .api]),
BenchmarkInfo(name: "ReversedDictionary", runFunction: run_ReversedDictionary, tags: [.validation, .api, .Dictionary]),
]
// These benchmarks compare the performance of iteration through several
// collection types after being reversed.
var x = 0
let length = 100_000
@inline(never)
public func run_ReversedArray(_ N: Int) {
let array = Array(repeating: 1, count: length)
let reversedArray = array.reversed()
// Iterate over the underlying type
// ReversedRandomAccessCollection<Array<Int>>
for _ in 1...N {
for item in reversedArray {
x = item
}
}
}
@inline(never)
public func run_ReversedBidirectional(_ N: Int) {
let bidirectional = AnyBidirectionalCollection(0..<length)
let reversedBidirectional = bidirectional.reversed()
// Iterate over the underlying type
// ReversedCollection<AnyBidirectionalCollection<Int>>
for _ in 1...N {
for item in reversedBidirectional {
x = item
}
}
}
@inline(never)
public func run_ReversedDictionary(_ N: Int) {
var dictionary = [Int: Int]()
for k in 0..<length {
dictionary[k] = x
}
let reversedDictionary = dictionary.reversed()
// Iterate over the underlying type
// Array<(Int, Int)>
for _ in 1...N {
for (key, value) in reversedDictionary {
x = key
x = value
}
}
}