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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
//===--- DictTest4.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
// This benchmark mostly measures lookups in dictionaries with complex keys,
// using the legacy hashValue API.
public let benchmarks = [
BenchmarkInfo(
name: "Dictionary4Legacy",
runFunction: run_Dictionary4Legacy,
tags: [.validation, .api, .Dictionary]),
BenchmarkInfo(
name: "Dictionary4OfObjectsLegacy",
runFunction: run_Dictionary4OfObjectsLegacy,
tags: [.validation, .api, .Dictionary]),
]
extension Int {
mutating func combine(_ value: Int) {
self = 16777619 &* self ^ value
}
}
struct LargeKey: Hashable {
let i: Int
let j: Int
let k: Double
let l: UInt32
let m: Bool
let n: Bool
let o: Bool
let p: Bool
let q: Bool
var hashValue: Int {
var hash = i.hashValue
hash.combine(j.hashValue)
hash.combine(k.hashValue)
hash.combine(l.hashValue)
hash.combine(m.hashValue)
hash.combine(n.hashValue)
hash.combine(o.hashValue)
hash.combine(p.hashValue)
hash.combine(q.hashValue)
return hash
}
func hash(into hasher: inout Hasher) {
hasher.combine(self.hashValue)
}
init(_ value: Int) {
self.i = value
self.j = 2 * value
self.k = Double(value) / 7
self.l = UInt32(truncatingIfNeeded: value)
self.m = value & 1 == 0
self.n = value & 2 == 0
self.o = value & 4 == 0
self.p = value & 8 == 0
self.q = value & 16 == 0
}
}
@inline(never)
public func run_Dictionary4Legacy(_ n: Int) {
let size1 = 100
let reps = 20
let ref_result = "1 99 \(reps) \(reps * 99)"
var hash1 = [LargeKey: Int]()
var hash2 = [LargeKey: Int]()
var res = ""
for _ in 1...n {
// Test insertions
hash1 = [:]
for i in 0..<size1 {
hash1[LargeKey(i)] = i
}
hash2 = hash1
// Test lookups & value replacement
for _ in 1..<reps {
for (k, v) in hash1 {
hash2[k] = hash2[k]! + v
}
}
res = "\(hash1[LargeKey(1)]!) \(hash1[LargeKey(99)]!)" +
" \(hash2[LargeKey(1)]!) \(hash2[LargeKey(99)]!)"
if res != ref_result {
break
}
}
check(res == ref_result)
}
class Box<T : Hashable> : Hashable {
var value: T
init(_ v: T) {
value = v
}
func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
var hashValue: Int {
return value.hashValue
}
static func ==(lhs: Box, rhs: Box) -> Bool {
return lhs.value == rhs.value
}
}
@inline(never)
public func run_Dictionary4OfObjectsLegacy(_ n: Int) {
let size1 = 100
let reps = 20
let ref_result = "1 99 \(reps) \(reps * 99)"
var hash1 = [Box<LargeKey>: Int]()
var hash2 = [Box<LargeKey>: Int]()
var res = ""
for _ in 1...n {
// Test insertions
hash1 = [:]
for i in 0..<size1 {
hash1[Box(LargeKey(i))] = i
}
hash2 = hash1
// Test lookups & value replacement
for _ in 1..<reps {
for (k, v) in hash1 {
hash2[k] = hash2[k]! + v
}
}
res = "\(hash1[Box(LargeKey(1))]!) \(hash1[Box(LargeKey(99))]!)" +
" \(hash2[Box(LargeKey(1))]!) \(hash2[Box(LargeKey(99))]!)"
if res != ref_result {
break
}
}
check(res == ref_result)
}
|