mirror of
https://github.com/apple/swift-collections
synced 2026-11-29 05:14:20 +00:00
Add reference benchmarks for bit vector implementations (#79)
* [benchmark] Reorganize C++/Foundation benchmarks into separate files * [benchmark] Add reference benchmarks for std::vector<bool> and CFBitVector * Fix thinko breaking non-Darwin builds
This commit is contained in:
1 parent
cc69069676
commit
f73d5e95a8
15 files changed
+1484
-711
No files matched your search
@@ -0,0 +1,44 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
extension Benchmark {
|
||||
public mutating func addCppBenchmarks() {
|
||||
cpp_set_hash_fn { value in value._rawHashValue(seed: 0) }
|
||||
|
||||
self.addSimple(
|
||||
title: "std::hash<intptr_t>",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_hash(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "custom_intptr_hash (using Swift.Hasher)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_custom_hash(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
_addCppVectorBenchmarks()
|
||||
_addCppDequeBenchmarks()
|
||||
_addCppUnorderedSetBenchmarks()
|
||||
_addCppUnorderedMapBenchmarks()
|
||||
_addCppPriorityQueueBenchmarks()
|
||||
_addCppVectorBoolBenchmarks()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
internal class CppDeque {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_deque_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Benchmark {
|
||||
internal mutating func _addCppDequeBenchmarks() {
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> push_back from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_deque_from_int_range(count)
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> constructor from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_from_int_buffer(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let deque = CppDeque(input)
|
||||
return { timer in
|
||||
cpp_deque_iterate(deque.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> random-access offset lookups (operator [])",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let vector = CppDeque(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_lookups_subscript(vector.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> at, random offsets",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let deque = CppDeque(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_lookups_at(deque.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> push_back",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_append_integers(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> push_front",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_prepend_integers(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> random insertions",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
insertions.values.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_random_insertions(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> pop_back",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let deque = CppDeque(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_deque_pop_back(deque.ptr)
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> pop_front",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let deque = CppDeque(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_deque_pop_front(deque.ptr)
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> random removals",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
let removals = Array(insertions.values.reversed())
|
||||
return { timer in
|
||||
let deque = CppDeque(Array(0 ..< removals.count))
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_random_removals(deque.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> sort",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let deque = CppDeque(input)
|
||||
timer.measure {
|
||||
cpp_deque_sort(deque.ptr)
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
internal class CppPriorityQueue {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_priority_queue_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
convenience init() {
|
||||
self.init([])
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_priority_queue_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
|
||||
func push(_ value: Int) {
|
||||
cpp_priority_queue_push(ptr, value)
|
||||
}
|
||||
|
||||
func push(_ values: [Int]) {
|
||||
values.withUnsafeBufferPointer { buffer in
|
||||
cpp_priority_queue_push_loop(ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
func pop() -> Int {
|
||||
cpp_priority_queue_pop(ptr)
|
||||
}
|
||||
|
||||
func popAll() {
|
||||
cpp_priority_queue_pop_all(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
extension Benchmark {
|
||||
internal mutating func _addCppPriorityQueueBenchmarks() {
|
||||
self.addSimple(
|
||||
title: "std::priority_queue<intptr_t> construct from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let pq = CppPriorityQueue(input)
|
||||
blackHole(pq)
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::priority_queue<intptr_t> push",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let pq = CppPriorityQueue()
|
||||
timer.measure {
|
||||
pq.push(input)
|
||||
}
|
||||
blackHole(pq)
|
||||
pq.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::priority_queue<intptr_t> pop",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let pq = CppPriorityQueue(input)
|
||||
timer.measure {
|
||||
pq.popAll()
|
||||
}
|
||||
blackHole(pq)
|
||||
pq.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
internal class CppUnorderedMap {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_unordered_map_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Benchmark {
|
||||
internal mutating func _addCppUnorderedMapBenchmarks() {
|
||||
self.addSimple(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> insert from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_unordered_map_from_int_range(count)
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let map = CppUnorderedMap(input)
|
||||
return { timer in
|
||||
cpp_unordered_map_iterate(map.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> successful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_lookups(map.ptr, buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> unsuccessful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
let lookups = lookups.map { $0 + input.count }
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_lookups(map.ptr, buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> subscript, existing key",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_subscript(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> subscript, new key",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
let lookups = lookups.map { $0 + input.count }
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_subscript(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> insert",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_insert_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> insert, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_insert_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> erase existing",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, removals in
|
||||
return { timer in
|
||||
let map = CppUnorderedMap(input)
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_removals(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
map.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> erase missing",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, removals in
|
||||
return { timer in
|
||||
let map = CppUnorderedMap(input.map { input.count + $0 })
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_removals(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
map.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
internal class CppUnorderedSet {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_unordered_set_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Benchmark {
|
||||
internal mutating func _addCppUnorderedSetBenchmarks() {
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> insert from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_unordered_set_from_int_range(count)
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> constructor from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_from_int_buffer(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let set = CppUnorderedSet(input)
|
||||
return { timer in
|
||||
cpp_unordered_set_iterate(set.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> successful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let set = CppUnorderedSet(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_lookups(set.ptr, buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> unsuccessful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let set = CppUnorderedSet(input)
|
||||
let lookups = lookups.map { $0 + input.count }
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_lookups(set.ptr, buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> insert",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_insert_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> insert, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_insert_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> erase",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, removals in
|
||||
return { timer in
|
||||
let set = CppUnorderedSet(input)
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_removals(set.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
set.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
internal class CppVector {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_vector_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Benchmark {
|
||||
internal mutating func _addCppVectorBenchmarks() {
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> push_back from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_vector_from_int_range(count)
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> constructor from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_from_int_buffer(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let vector = CppVector(input)
|
||||
return { timer in
|
||||
cpp_vector_iterate(vector.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> random-access offset lookups (operator [])",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let vector = CppVector(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_lookups_subscript(vector.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> random-access offset lookups (at)",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let deque = CppVector(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_lookups_at(deque.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> push_back",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_append_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> push_back, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_append_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> insert at front",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_prepend_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> insert at front, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_prepend_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> random insertions",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
insertions.values.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_random_insertions(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> pop_back",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let vector = CppVector(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_vector_pop_back(vector.ptr)
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> erase first",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let vector = CppVector(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_vector_pop_front(vector.ptr)
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> random removals",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
let removals = Array(insertions.values.reversed())
|
||||
return { timer in
|
||||
let vector = CppVector(Array(0 ..< removals.count))
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_random_removals(vector.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> sort",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let vector = CppVector(input)
|
||||
timer.measure {
|
||||
cpp_vector_sort(vector.ptr)
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
internal class CppVectorBool {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(repeating value: Bool, count: Int) {
|
||||
self.ptr = cpp_vector_bool_create_repeating(count, value)
|
||||
}
|
||||
|
||||
init(count: Int, trueBits: ArraySlice<Int>) {
|
||||
self.ptr = cpp_vector_bool_create_repeating(count, false)
|
||||
trueBits.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_set_indices_subscript(
|
||||
self.ptr,
|
||||
buffer.baseAddress,
|
||||
buffer.count)
|
||||
}
|
||||
withExtendedLifetime(self) {}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_vector_bool_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
extension Array where Element == Bool {
|
||||
init<S: Sequence>(count: Int, trueBits: S) where S.Element == Int {
|
||||
self.init(repeating: false, count: count)
|
||||
for index in trueBits {
|
||||
self[index] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Benchmark {
|
||||
internal mutating func _addCppVectorBoolBenchmarks() {
|
||||
self.addSimple(
|
||||
title: "std::vector<bool> create from integer buffer (subscript)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(repeating: false, count: input.count)
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_set_indices_subscript(
|
||||
v.ptr,
|
||||
buffer.baseAddress,
|
||||
trueCount)
|
||||
}
|
||||
v.destroy()
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<bool> create from integer buffer (at)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(repeating: false, count: input.count)
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_set_indices_at(
|
||||
v.ptr,
|
||||
buffer.baseAddress,
|
||||
trueCount)
|
||||
}
|
||||
v.destroy()
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> const_iterator",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(count: input.count, trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
cpp_vector_bool_iterate(identity(v.ptr))
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> find true bits",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(count: input.count, trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
precondition(
|
||||
cpp_vector_bool_find_true_bits(identity(v.ptr)) == trueCount)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> count true bits",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(count: input.count, trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
precondition(
|
||||
cpp_vector_bool_count_true_bits(identity(v.ptr)) == trueCount)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> random-access offset lookups (subscript)",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(count: input.count, trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_lookups_subscript(v.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> random-access offset lookups (at)",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(count: input.count, trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_lookups_at(v.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> set bits to true (subscript)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let v = CppVectorBool(repeating: false, count: input.count)
|
||||
return { timer in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_set_indices_subscript(v.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> set bits to true (at)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let v = CppVectorBool(repeating: false, count: input.count)
|
||||
return { timer in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_set_indices_at(v.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> set bits to false (subscript)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let v = CppVectorBool(repeating: true, count: input.count)
|
||||
return { timer in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_reset_indices_subscript(v.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> set bits to false (at)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let v = CppVectorBool(repeating: true, count: input.count)
|
||||
return { timer in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_reset_indices_at(v.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> push_back",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let trueCount = input.count / 2
|
||||
let bools = Array<Bool>(count: input.count, trueBits: input[..<trueCount])
|
||||
|
||||
return { timer in
|
||||
bools.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_push_back(buffer.baseAddress, buffer.count, /*reserve*/false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> push_back, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let trueCount = input.count / 2
|
||||
let bools = Array<Bool>(count: input.count, trueBits: input[..<trueCount])
|
||||
|
||||
return { timer in
|
||||
bools.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_bool_push_back(buffer.baseAddress, buffer.count, /*reserve*/true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::vector<bool> pop_back",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let trueCount = input.count / 2
|
||||
let v = CppVectorBool(count: input.count, trueBits: input[..<trueCount])
|
||||
timer.measure {
|
||||
cpp_vector_bool_pop_back(v.ptr, input.count)
|
||||
}
|
||||
v.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,710 +0,0 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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 CppBenchmarks
|
||||
|
||||
internal class CppVector {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_vector_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
internal class CppDeque {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_deque_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
internal class CppUnorderedSet {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_unordered_set_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
internal class CppUnorderedMap {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_unordered_map_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
}
|
||||
|
||||
internal class CppPriorityQueue {
|
||||
var ptr: UnsafeMutableRawPointer?
|
||||
|
||||
init(_ input: [Int]) {
|
||||
self.ptr = input.withUnsafeBufferPointer { buffer in
|
||||
cpp_priority_queue_create(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
convenience init() {
|
||||
self.init([])
|
||||
}
|
||||
|
||||
deinit {
|
||||
destroy()
|
||||
}
|
||||
|
||||
func destroy() {
|
||||
if let ptr = ptr {
|
||||
cpp_priority_queue_destroy(ptr)
|
||||
}
|
||||
ptr = nil
|
||||
}
|
||||
|
||||
func push(_ value: Int) {
|
||||
cpp_priority_queue_push(ptr, value)
|
||||
}
|
||||
|
||||
func push(_ values: [Int]) {
|
||||
values.withUnsafeBufferPointer { buffer in
|
||||
cpp_priority_queue_push_loop(ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
func pop() -> Int {
|
||||
cpp_priority_queue_pop(ptr)
|
||||
}
|
||||
|
||||
func popAll() {
|
||||
cpp_priority_queue_pop_all(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Benchmark {
|
||||
public mutating func addCppBenchmarks() {
|
||||
cpp_set_hash_fn { value in value._rawHashValue(seed: 0) }
|
||||
|
||||
self.addSimple(
|
||||
title: "std::hash<intptr_t>",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_hash(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "custom_intptr_hash (using Swift.Hasher)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_custom_hash(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> push_back from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_vector_from_int_range(count)
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> push_back from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_deque_from_int_range(count)
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> constructor from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_from_int_buffer(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> constructor from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_from_int_buffer(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let vector = CppVector(input)
|
||||
return { timer in
|
||||
cpp_vector_iterate(vector.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let deque = CppDeque(input)
|
||||
return { timer in
|
||||
cpp_deque_iterate(deque.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> random-access offset lookups (operator [])",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let vector = CppVector(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_lookups_subscript(vector.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> random-access offset lookups (operator [])",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let vector = CppDeque(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_lookups_subscript(vector.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> random-access offset lookups (at)",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let deque = CppVector(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_lookups_at(deque.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> at, random offsets",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let deque = CppDeque(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_lookups_at(deque.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> push_back",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_append_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> push_back, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_append_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> push_back",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_append_integers(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> insert at front",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_prepend_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> insert at front, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_prepend_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> push_front",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_prepend_integers(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.addSimple(
|
||||
title: "std::vector<intptr_t> random insertions",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
insertions.values.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_random_insertions(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::deque<intptr_t> random insertions",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
insertions.values.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_random_insertions(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> pop_back",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let vector = CppVector(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_vector_pop_back(vector.ptr)
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> pop_back",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let deque = CppDeque(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_deque_pop_back(deque.ptr)
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> erase first",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let vector = CppVector(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_vector_pop_front(vector.ptr)
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> pop_front",
|
||||
input: Int.self
|
||||
) { size in
|
||||
return { timer in
|
||||
let deque = CppDeque(Array(0 ..< size))
|
||||
timer.measure {
|
||||
cpp_deque_pop_front(deque.ptr)
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> random removals",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
let removals = Array(insertions.values.reversed())
|
||||
return { timer in
|
||||
let vector = CppVector(Array(0 ..< removals.count))
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_vector_random_removals(vector.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> random removals",
|
||||
input: Insertions.self
|
||||
) { insertions in
|
||||
let removals = Array(insertions.values.reversed())
|
||||
return { timer in
|
||||
let deque = CppDeque(Array(0 ..< removals.count))
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_deque_random_removals(deque.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.add(
|
||||
title: "std::vector<intptr_t> sort",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let vector = CppVector(input)
|
||||
timer.measure {
|
||||
cpp_vector_sort(vector.ptr)
|
||||
}
|
||||
vector.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::deque<intptr_t> sort",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let deque = CppDeque(input)
|
||||
timer.measure {
|
||||
cpp_deque_sort(deque.ptr)
|
||||
}
|
||||
deque.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> insert from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_unordered_set_from_int_range(count)
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> constructor from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_from_int_buffer(buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let set = CppUnorderedSet(input)
|
||||
return { timer in
|
||||
cpp_unordered_set_iterate(set.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> successful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let set = CppUnorderedSet(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_lookups(set.ptr, buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> unsuccessful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let set = CppUnorderedSet(input)
|
||||
let lookups = lookups.map { $0 + input.count }
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_lookups(set.ptr, buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> insert",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_insert_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_set<intptr_t> insert, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_insert_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_set<intptr_t> erase",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, removals in
|
||||
return { timer in
|
||||
let set = CppUnorderedSet(input)
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_set_removals(set.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
set.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> insert from integer range",
|
||||
input: Int.self
|
||||
) { count in
|
||||
cpp_unordered_map_from_int_range(count)
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> sequential iteration",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let set = CppUnorderedMap(input)
|
||||
return { timer in
|
||||
cpp_unordered_map_iterate(set.ptr)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> successful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_lookups(map.ptr, buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> unsuccessful find",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
let lookups = lookups.map { $0 + input.count }
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_lookups(map.ptr, buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> subscript, existing key",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_subscript(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> subscript, new key",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
let map = CppUnorderedMap(input)
|
||||
let lookups = lookups.map { $0 + input.count }
|
||||
return { timer in
|
||||
lookups.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_subscript(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> insert",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_insert_integers(buffer.baseAddress, buffer.count, false)
|
||||
}
|
||||
}
|
||||
|
||||
self.addSimple(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> insert, reserving capacity",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
input.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_insert_integers(buffer.baseAddress, buffer.count, true)
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> erase existing",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, removals in
|
||||
return { timer in
|
||||
let map = CppUnorderedMap(input)
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_removals(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
map.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::unordered_map<intptr_t, intptr_t> erase missing",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, removals in
|
||||
return { timer in
|
||||
let map = CppUnorderedMap(input.map { input.count + $0 })
|
||||
timer.measure {
|
||||
removals.withUnsafeBufferPointer { buffer in
|
||||
cpp_unordered_map_removals(map.ptr, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
}
|
||||
map.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
self.addSimple(
|
||||
title: "std::priority_queue<intptr_t> construct from buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
let pq = CppPriorityQueue(input)
|
||||
blackHole(pq)
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::priority_queue<intptr_t> push",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let pq = CppPriorityQueue()
|
||||
timer.measure {
|
||||
pq.push(input)
|
||||
}
|
||||
blackHole(pq)
|
||||
pq.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "std::priority_queue<intptr_t> pop",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
return { timer in
|
||||
let pq = CppPriorityQueue(input)
|
||||
timer.measure {
|
||||
pq.popAll()
|
||||
}
|
||||
blackHole(pq)
|
||||
pq.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -37,7 +37,7 @@ extension CFBinaryHeap {
|
||||
#endif
|
||||
|
||||
extension Benchmark {
|
||||
public mutating func addFoundationBenchmarks() {
|
||||
internal mutating func _addCFBinaryHeapBenchmarks() {
|
||||
self.add(
|
||||
title: "CFBinaryHeapAddValue",
|
||||
input: [Int].self
|
||||
@@ -0,0 +1,161 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
import Foundation
|
||||
|
||||
extension CFMutableBitVector {
|
||||
static func _create<S: Sequence>(
|
||||
count: Int,
|
||||
trueBits: S
|
||||
) -> CFMutableBitVector where S.Element == Int {
|
||||
let bits = CFBitVectorCreateMutable(nil, count)!
|
||||
CFBitVectorSetCount(bits, count)
|
||||
for index in trueBits {
|
||||
CFBitVectorSetBitAtIndex(bits, index, 1)
|
||||
}
|
||||
return bits
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
extension Benchmark {
|
||||
internal mutating func _addCFBitVectorBenchmarks() {
|
||||
self.add(
|
||||
title: "CFBitVector create from integer buffer",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
return { timer in
|
||||
let trueCount = input.count / 2
|
||||
let bits = CFMutableBitVector._create(
|
||||
count: input.count,
|
||||
trueBits: input[..<trueCount])
|
||||
blackHole(bits)
|
||||
}
|
||||
#else
|
||||
// CFBitVector isn't available
|
||||
return nil
|
||||
#endif
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "CFBitVectorGetBitAtIndex (sequential iteration)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
let trueCount = input.count / 2
|
||||
let bits = CFMutableBitVector._create(
|
||||
count: input.count,
|
||||
trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
for index in 0 ..< input.count {
|
||||
blackHole(CFBitVectorGetBitAtIndex(bits, index))
|
||||
}
|
||||
}
|
||||
#else
|
||||
// CFBitVector isn't available
|
||||
return nil
|
||||
#endif
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "CFBitVectorGetBitAtIndex (random-access lookups)",
|
||||
input: ([Int], [Int]).self
|
||||
) { input, lookups in
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
let trueCount = input.count / 2
|
||||
let bits = CFMutableBitVector._create(
|
||||
count: input.count,
|
||||
trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
for index in lookups {
|
||||
blackHole(CFBitVectorGetBitAtIndex(bits, index))
|
||||
}
|
||||
}
|
||||
#else
|
||||
// CFBitVector isn't available
|
||||
return nil
|
||||
#endif
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "CFBitVectorGetFirstIndexOfBit",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
let trueCount = input.count / 2
|
||||
let bits = CFMutableBitVector._create(
|
||||
count: input.count,
|
||||
trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
var c = 0
|
||||
var index = 0
|
||||
while true {
|
||||
let range = CFRangeMake(index, input.count - index)
|
||||
index = CFBitVectorGetFirstIndexOfBit(bits, range, 1)
|
||||
guard index != kCFNotFound else { break }
|
||||
c += 1
|
||||
index += 1
|
||||
}
|
||||
precondition(c == trueCount, "count: \(input.count), found: \(c), expected: \(trueCount)")
|
||||
}
|
||||
#else
|
||||
// CFBitVector isn't available
|
||||
return nil
|
||||
#endif
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "CFBitVectorGetCountOfBit",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
let trueCount = input.count / 2
|
||||
let bits = CFMutableBitVector._create(
|
||||
count: input.count,
|
||||
trueBits: input[..<trueCount])
|
||||
return { timer in
|
||||
let range = CFRangeMake(0, input.count)
|
||||
let c = CFBitVectorGetCountOfBit(bits, range, 1)
|
||||
precondition(c == trueCount)
|
||||
}
|
||||
#else
|
||||
// CFBitVector isn't available
|
||||
return nil
|
||||
#endif
|
||||
}
|
||||
|
||||
self.add(
|
||||
title: "CFBitVectorSetBitAtIndex (random-access set)",
|
||||
input: [Int].self
|
||||
) { input in
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
|
||||
return { timer in
|
||||
let bits = CFBitVectorCreateMutable(nil, input.count)
|
||||
CFBitVectorSetCount(bits, input.count)
|
||||
timer.measure {
|
||||
for index in input {
|
||||
CFBitVectorSetBitAtIndex(bits, index, 1)
|
||||
}
|
||||
}
|
||||
blackHole(bits)
|
||||
}
|
||||
#else
|
||||
// CFBitVector isn't available
|
||||
return nil
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
|
||||
extension Benchmark {
|
||||
public mutating func addFoundationBenchmarks() {
|
||||
_addCFBinaryHeapBenchmarks()
|
||||
_addCFBitVectorBenchmarks()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef CPPBENCHMARKS_VECTOR_BOOL_BENCHMARKS_H
|
||||
#define CPPBENCHMARKS_VECTOR_BOOL_BENCHMARKS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/// Create a `std::vector<bool>` of the specified size, filling it with the
|
||||
/// given value. Returns an opaque pointer to the created instance.
|
||||
extern void *cpp_vector_bool_create_repeating(size_t count, bool value);
|
||||
|
||||
/// Destroy a vector previously returned by `cpp_vector_bool_create`.
|
||||
extern void cpp_vector_bool_destroy(void *ptr);
|
||||
|
||||
extern void cpp_vector_bool_push_back(const bool *start, size_t count, bool reserve);
|
||||
|
||||
extern void cpp_vector_bool_pop_back(void *ptr, size_t count);
|
||||
|
||||
/// Set bits indexed by a buffer of integers to true, using the unchecked subscript.
|
||||
extern void cpp_vector_bool_set_indices_subscript(void *ptr, const intptr_t *start, size_t count);
|
||||
|
||||
/// Set bits indexed by a buffer of integers to true, using the checked `at` method.
|
||||
extern void cpp_vector_bool_set_indices_at(void *ptr, const intptr_t *start, size_t count);
|
||||
|
||||
/// Set bits indexed by a buffer of integers to false, using the unchecked subscript.
|
||||
extern void cpp_vector_bool_reset_indices_subscript(void *ptr, const intptr_t *start, size_t count);
|
||||
|
||||
/// Set bits indexed by a buffer of integers to false, using the checked `at` method.
|
||||
extern void cpp_vector_bool_reset_indices_at(void *ptr, const intptr_t *start, size_t count);
|
||||
|
||||
/// Retrieve all bits indexed by a buffer of integers, using the unchecked subscript.
|
||||
extern void cpp_vector_bool_lookups_subscript(void *ptr, const intptr_t *start, size_t count);
|
||||
|
||||
/// Retrieve all bits indexed by a buffer of integers, using the checked `at` method.
|
||||
extern void cpp_vector_bool_lookups_at(void *ptr, const intptr_t *start, size_t count);
|
||||
|
||||
/// Iterate through all the bits in a `vector<bool>`.
|
||||
extern void cpp_vector_bool_iterate(void *ptr);
|
||||
|
||||
/// Use `std::find` to visit every true bit in a `vector<bool>`, returning
|
||||
/// the number of true bits found.
|
||||
extern size_t cpp_vector_bool_find_true_bits(void *ptr);
|
||||
|
||||
/// Use `std::count` to return a count of every true bit in a `vector<bool>`.
|
||||
extern size_t cpp_vector_bool_count_true_bits(void *ptr);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* CPPBENCHMARKS_VECTOR_BOOL_BENCHMARKS_H */
|
||||
@@ -6,5 +6,6 @@ module CppBenchmarks {
|
||||
header "UnorderedSetBenchmarks.h"
|
||||
header "UnorderedMapBenchmarks.h"
|
||||
header "PriorityQueueBenchmarks.h"
|
||||
header "VectorBoolBenchmarks.h"
|
||||
export *
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include "VectorBoolBenchmarks.h"
|
||||
#include "Utils.h"
|
||||
|
||||
void *
|
||||
cpp_vector_bool_create_repeating(size_t count, bool value)
|
||||
{
|
||||
auto v = new std::vector<bool>(count, value);
|
||||
return v;
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_destroy(void *ptr)
|
||||
{
|
||||
delete static_cast<std::vector<bool> *>(ptr);
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_push_back(const bool *start, size_t count, bool reserve)
|
||||
{
|
||||
auto v = std::vector<bool>();
|
||||
|
||||
if (reserve) {
|
||||
v.reserve(v.size() + count);
|
||||
}
|
||||
|
||||
for (auto p = start; p < start + count; ++p) {
|
||||
v.push_back(*p);
|
||||
}
|
||||
|
||||
black_hole(&v);
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_pop_back(void *ptr, size_t count)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (size_t i = 0; i < count; ++i) {
|
||||
v.pop_back();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_set_indices_subscript(void *ptr, const intptr_t *start, size_t count)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (auto p = start; p < start + count; ++p) {
|
||||
v[*p] = true;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_set_indices_at(void *ptr, const intptr_t *start, size_t count)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (auto p = start; p < start + count; ++p) {
|
||||
v.at(*p) = true;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_reset_indices_subscript(void *ptr, const intptr_t *start, size_t count)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (auto p = start; p < start + count; ++p) {
|
||||
v[*p] = false;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_reset_indices_at(void *ptr, const intptr_t *start, size_t count)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (auto p = start; p < start + count; ++p) {
|
||||
v.at(*p) = false;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_lookups_subscript(void *ptr, const intptr_t *start, size_t count)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (auto p = start; p < start + count; ++p) {
|
||||
black_hole(v[*p]);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_lookups_at(void *ptr, const intptr_t *start, size_t count)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (auto p = start; p < start + count; ++p) {
|
||||
black_hole(v.at(*p));
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
cpp_vector_bool_iterate(void *ptr)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
for (auto it = v.cbegin(); it != v.cend(); ++it) {
|
||||
black_hole(*it);
|
||||
}
|
||||
}
|
||||
|
||||
size_t
|
||||
cpp_vector_bool_find_true_bits(void *ptr)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
size_t count = 0;
|
||||
for (auto it = std::find(v.cbegin(), v.cend(), true);
|
||||
it != v.cend();
|
||||
it = std::find(it, v.cend(), true)) {
|
||||
++count;
|
||||
++it;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t
|
||||
cpp_vector_bool_count_true_bits(void *ptr)
|
||||
{
|
||||
auto &v = *static_cast<std::vector<bool> *>(ptr);
|
||||
|
||||
return std::count(v.cbegin(), v.cend(), true);
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
{
|
||||
"kind": "group",
|
||||
"title": "Bitset Benchmarks",
|
||||
"directory": "Results",
|
||||
"contents": [
|
||||
{
|
||||
"kind": "group",
|
||||
"title": "BitSet Operations",
|
||||
"contents": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": "group",
|
||||
"title": "BitArray Operations",
|
||||
"contents": [
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": "group",
|
||||
"title": "Comparisons against reference implementations",
|
||||
"directory": "versus",
|
||||
"contents": [
|
||||
{
|
||||
"kind": "chart",
|
||||
"title": "initializers",
|
||||
"tasks": [
|
||||
"std::vector<bool> create from integer buffer (subscript)",
|
||||
"std::vector<bool> create from integer buffer (at)",
|
||||
"CFBitVector create from integer buffer"
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": "chart",
|
||||
"title": "sequential iteration",
|
||||
"tasks": [
|
||||
"std::vector<bool> const_iterator",
|
||||
"CFBitVectorGetBitAtIndex (sequential iteration)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": "chart",
|
||||
"title": "random-access lookups",
|
||||
"tasks": [
|
||||
"std::vector<bool> random-access offset lookups (subscript)",
|
||||
"std::vector<bool> random-access offset lookups (at)",
|
||||
"CFBitVectorGetBitAtIndex (random-access lookups)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": "chart",
|
||||
"title": "set random bits to true",
|
||||
"tasks": [
|
||||
"std::vector<bool> set bits to true (subscript)",
|
||||
"std::vector<bool> set bits to true (at)",
|
||||
"CFBitVectorSetBitAtIndex (random-access set)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": "chart",
|
||||
"title": "find true bits",
|
||||
"tasks": [
|
||||
"std::vector<bool> find true bits",
|
||||
"CFBitVectorGetFirstIndexOfBit"
|
||||
]
|
||||
},
|
||||
{
|
||||
"kind": "chart",
|
||||
"title": "count true bits",
|
||||
"tasks": [
|
||||
"std::vector<bool> count true bits",
|
||||
"CFBitVectorGetCountOfBit"
|
||||
]
|
||||
},
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user