1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
//===--- DictionaryBridgeToObjC.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
//
//===----------------------------------------------------------------------===//
// Performance benchmark for common operations on Dictionary values bridged to
// NSDictionary.
import TestsUtils
#if _runtime(_ObjC)
import Foundation
public let benchmarks = [
BenchmarkInfo(
name: "DictionaryBridgeToObjC_Bridge",
runFunction: run_DictionaryBridgeToObjC_BridgeToObjC,
tags: [.validation, .api, .Dictionary, .bridging]),
BenchmarkInfo(
name: "DictionaryBridgeToObjC_Access",
runFunction: run_DictionaryBridgeToObjC_Access,
tags: [.validation, .api, .Dictionary, .bridging]),
BenchmarkInfo(
name: "DictionaryBridgeToObjC_BulkAccess",
runFunction: run_DictionaryBridgeToObjC_BulkAccess,
tags: [.validation, .api, .Dictionary, .bridging])
]
let numbers: [String: Int] = [
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
"ten": 10,
"eleven": 11,
"twelve": 12,
"thirteen": 13,
"fourteen": 14,
"fifteen": 15,
"sixteen": 16,
"seventeen": 17,
"eighteen": 18,
"nineteen": 19,
"twenty": 20
]
@inline(never)
public func run_DictionaryBridgeToObjC_BridgeToObjC(_ n: Int) {
for _ in 1 ... 100 * n {
blackHole(numbers as NSDictionary)
}
}
@inline(never)
public func run_DictionaryBridgeToObjC_Access(_ n: Int) {
let d = numbers as NSDictionary
blackHole(d.object(forKey: "one")) // Force bridging of contents
for _ in 1 ... 100 * n {
for key in numbers.keys {
check(identity(d).object(forKey: key) != nil)
}
}
}
@inline(never)
public func run_DictionaryBridgeToObjC_BulkAccess(_ n: Int) {
let d = numbers as NSDictionary
for _ in 1 ... 100 * n {
let d2 = NSDictionary(dictionary: identity(d))
check(d2.count == d.count)
}
}
#else // !_runtime(ObjC)
public let benchmarks: [BenchmarkInfo] = []
#endif
|