-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Capsule] Copy
DictionaryBenchmark
and add some Dictionary
API
- Loading branch information
1 parent
e8dbd8d
commit 37e957f
Showing
5 changed files
with
382 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,343 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift Collections open source project | ||
// | ||
// Copyright (c) 2021 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import CollectionsBenchmark | ||
import Capsule | ||
|
||
extension Benchmark { | ||
public mutating func addCapsuleHashMapBenchmarks() { | ||
self.add( | ||
title: "HashMap<Int, Int> init(uniqueKeysWithValues:)", | ||
input: [Int].self | ||
) { input in | ||
let keysAndValues = input.map { ($0, 2 * $0) } | ||
return { timer in | ||
blackHole(HashMap(uniqueKeysWithValues: keysAndValues)) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> sequential iteration", | ||
input: [Int].self | ||
) { input in | ||
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
return { timer in | ||
for item in d { | ||
blackHole(item) | ||
} | ||
} | ||
} | ||
|
||
// self.add( | ||
// title: "HashMap<Int, Int>.Keys sequential iteration", | ||
// input: [Int].self | ||
// ) { input in | ||
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
// return { timer in | ||
// for item in d.keys { | ||
// blackHole(item) | ||
// } | ||
// } | ||
// } | ||
// | ||
// self.add( | ||
// title: "HashMap<Int, Int>.Values sequential iteration", | ||
// input: [Int].self | ||
// ) { input in | ||
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
// return { timer in | ||
// for item in d.values { | ||
// blackHole(item) | ||
// } | ||
// } | ||
// } | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> subscript, successful lookups", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
return { timer in | ||
for i in lookups { | ||
precondition(d[i] == 2 * i) | ||
} | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> subscript, unsuccessful lookups", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
let c = input.count | ||
return { timer in | ||
for i in lookups { | ||
precondition(d[i + c] == nil) | ||
} | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> subscript, noop setter", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
let c = input.count | ||
timer.measure { | ||
for i in lookups { | ||
d[i + c] = nil | ||
} | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> subscript, set existing", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
timer.measure { | ||
for i in lookups { | ||
d[i] = 0 | ||
} | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> subscript, _modify", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
timer.measure { | ||
for i in lookups { | ||
d[i]! *= 2 | ||
} | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.addSimple( | ||
title: "HashMap<Int, Int> subscript, insert", | ||
input: [Int].self | ||
) { input in | ||
var d: [Int: Int] = [:] | ||
for i in input { | ||
d[i] = 2 * i | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
|
||
self.addSimple( | ||
title: "HashMap<Int, Int> subscript, insert, reserving capacity", | ||
input: [Int].self | ||
) { input in | ||
var d: [Int: Int] = [:] | ||
d.reserveCapacity(input.count) | ||
for i in input { | ||
d[i] = 2 * i | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> subscript, remove existing", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
timer.measure { | ||
for i in lookups { | ||
d[i] = nil | ||
} | ||
} | ||
precondition(d.isEmpty) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> subscript, remove missing", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
let c = input.count | ||
timer.measure { | ||
for i in lookups { | ||
d[i + c] = nil | ||
} | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> defaulted subscript, successful lookups", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
return { timer in | ||
for i in lookups { | ||
precondition(d[i, default: -1] != -1) | ||
} | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> defaulted subscript, unsuccessful lookups", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
return { timer in | ||
let c = d.count | ||
for i in lookups { | ||
precondition(d[i + c, default: -1] == -1) | ||
} | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> defaulted subscript, _modify existing", | ||
input: [Int].self | ||
) { input in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
timer.measure { | ||
for i in input { | ||
d[i, default: -1] *= 2 | ||
} | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> defaulted subscript, _modify missing", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
let c = input.count | ||
timer.measure { | ||
for i in lookups { | ||
d[c + i, default: -1] *= 2 | ||
} | ||
} | ||
precondition(d.count == 2 * input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
// self.add( | ||
// title: "HashMap<Int, Int> successful index(forKey:)", | ||
// input: ([Int], [Int]).self | ||
// ) { input, lookups in | ||
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
// return { timer in | ||
// for i in lookups { | ||
// precondition(d.index(forKey: i) != nil) | ||
// } | ||
// } | ||
// } | ||
// | ||
// self.add( | ||
// title: "HashMap<Int, Int> unsuccessful index(forKey:)", | ||
// input: ([Int], [Int]).self | ||
// ) { input, lookups in | ||
// let d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
// return { timer in | ||
// for i in lookups { | ||
// precondition(d.index(forKey: lookups.count + i) == nil) | ||
// } | ||
// } | ||
// } | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> updateValue(_:forKey:), existing", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
timer.measure { | ||
for i in lookups { | ||
d.updateValue(0, forKey: i) | ||
} | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> updateValue(_:forKey:), insert", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
timer.measure { | ||
for i in lookups { | ||
d.updateValue(0, forKey: input.count + i) | ||
} | ||
} | ||
precondition(d.count == 2 * input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> random removals (existing keys)", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { ($0, 2 * $0) }) | ||
timer.measure { | ||
for i in lookups { | ||
precondition(d.removeValue(forKey: i) != nil) | ||
} | ||
} | ||
precondition(d.count == 0) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
self.add( | ||
title: "HashMap<Int, Int> random removals (missing keys)", | ||
input: ([Int], [Int]).self | ||
) { input, lookups in | ||
return { timer in | ||
let c = input.count | ||
var d = HashMap(uniqueKeysWithValues: input.lazy.map { (c + $0, 2 * $0) }) | ||
timer.measure { | ||
for i in lookups { | ||
precondition(d.removeValue(forKey: i) == nil) | ||
} | ||
} | ||
precondition(d.count == input.count) | ||
blackHole(d) | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.