Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BijectiveDictionaryBenchmark #22

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 96 additions & 35 deletions Sources/BijectiveDictionaryBenchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,61 +3,122 @@ import CollectionsBenchmark

var benchmark = Benchmark(title: "BijectiveDictionary Benchmark")

// MARK: Input Generators
benchmark.registerInputGenerator(
for: [(left: Int, right: Int)].self
) { size in
return (0..<size).map { index in
return (left: index, right: index)
}
}

benchmark.registerInputGenerator(
for: [Int: Int].self
) { size in
let keys = Set(0..<size)
let values = keys
let pairs = zip(keys, values)
return Dictionary(uniqueKeysWithValues: pairs)
}

benchmark.registerInputGenerator(
for: (Int, [Int]).self
) { size in
let int = size
let arrayOfInt = Array(0..<size)
return (int, arrayOfInt)
}

// MARK: Benchmarks
// MARK: Initializers
benchmark.addSimple(
title: "BijectiveDictionary<Int, Int> init minimum capacity",
input: Int.self
) { input in
blackHole(BijectiveDictionary<Int, Int>(minimumCapacity: input))
}

benchmark.addSimple(
title: "BijectiveDictionary<Int, Int> init from Dictionary value",
input: [Int: Int].self
) { input in
blackHole(BijectiveDictionary<Int, Int>(input))
}

benchmark.add(
title: "BijectiveDictionary<Int, Int> init unique left-right pairs",
input: Int.self
) { input in
let leftValues = Array(0..<input) // positive values
let rightValues = Array(-input..<0) // negative values
let pairs = zip(leftValues, rightValues)
return { timer in
blackHole(BijectiveDictionary<Int, Int>(uniqueLeftRightPairs: pairs))
}
}

extension Sequence where Element == (left: String, right: Int) {
func insertedByLeftIntoBijectiveDictionary() -> BijectiveDictionary<String, Int> {
var bDict = BijectiveDictionary<String, Int>(minimumCapacity: underestimatedCount)
for element in self {
// MARK: Iteration
benchmark.add(
title: "BijectiveDictionary<Int, Int> sequential iteration",
input: [(left: Int, right: Int)].self
) { input in
let bDict = BijectiveDictionary<Int, Int>(uniqueLeftRightPairs: input)
return { timer in
for element in bDict {
blackHole(element)
}
}
}

// MARK: Insertion
benchmark.addSimple(
title: "BijectiveDictionary<Int, Int> insertion into left",
input: [(left: Int, right: Int)].self
) { input in
let insertedByLeftIntoBijectiveDictionary = {
var bDict = BijectiveDictionary<Int, Int>(minimumCapacity: input.underestimatedCount)
for element in input {
bDict[left: element.left] = element.right
}
return bDict
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Functions are now written directly inline in the benchmark tests to improve readability.

func insertedByRightIntoBijectiveDictionary() -> BijectiveDictionary<String, Int> {
var bDict = BijectiveDictionary<String, Int>(minimumCapacity: underestimatedCount)
for element in self {
blackHole(insertedByLeftIntoBijectiveDictionary())
}

benchmark.addSimple(
title: "BijectiveDictionary<Int, Int> insertion into right",
input: [(left: Int, right: Int)].self
) { input in
let insertedByRightIntoBijectiveDictionary = {
var bDict = BijectiveDictionary<Int, Int>(minimumCapacity: input.underestimatedCount)
for element in input {
bDict[right: element.right] = element.left
}
return bDict
}

func insertedIntoDictionary() -> Dictionary<String, Int> {
var dict = Dictionary<String, Int>(minimumCapacity: underestimatedCount)
for element in self {
blackHole(insertedByRightIntoBijectiveDictionary())
}

benchmark.addSimple(
title: "Dictionary<Int, Int> insertion",
input: [(left: Int, right: Int)].self
) { input in
let insertedIntoDictionary = {
var dict = Dictionary<Int, Int>(minimumCapacity: input.underestimatedCount)
for element in input {
dict[element.left] = element.right
}
return dict
}
blackHole(insertedIntoDictionary())
}

// MARK: Removal


benchmark.registerInputGenerator(
for: [(left: String, right: Int)].self) { size in
return (0..<size).map { index in
return (left: "String\(index)", right: index)
}
}

benchmark.addSimple(
title: "BijectiveDictionary<String, Int> insertion into left",
input: [(left: String, right: Int)].self
) { input in
blackHole(input.insertedByLeftIntoBijectiveDictionary())
}

benchmark.addSimple(
title: "BijectiveDictionary<String, Int> insertion into right",
input: [(left: String, right: Int)].self
) { input in
blackHole(input.insertedByRightIntoBijectiveDictionary())
}

benchmark.addSimple(
title: "Dictionary<String, Int> insertion",
input: [(left: String, right: Int)].self
) { input in
blackHole(input.insertedIntoDictionary())
}

benchmark.main()
Binary file added chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions results

Large diffs are not rendered by default.