Skip to content

Instantly share code, notes, and snippets.

@karwa
Last active November 4, 2021 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karwa/cc009cbea71c83e7102b9594f66564db to your computer and use it in GitHub Desktop.
Save karwa/cc009cbea71c83e7102b9594f66564db to your computer and use it in GitHub Desktop.
// Use the following Package.swift:
// -----------------------------------------------------------
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
//
//import PackageDescription
//
//let package = Package(
// name: "uniqueid-test",
// dependencies: [
// .package(url: "https://github.com/karwa/uniqueid", .upToNextMajor(from: "1.0.1")),
// .package(url: "https://github.com/google/swift-benchmark", from: "0.1.0"),
// ],
// targets: [
// .executableTarget(
// name: "uniqueid-test",
// dependencies: [
// .product(name: "UniqueID", package: "uniqueid"),
// .product(name: "Benchmark", package: "swift-benchmark"),
// ])
// ]
//)
// -----------------------------------------------------------
import Benchmark
import UniqueID
import Foundation
@inline(never) @_optimize(none)
func blackHole<T>(_: T) {}
let NumberOfUUIDs = 10_000
let numberOfTasks = 32
let uuidsPerTask = 100_000
extension UniqueID {
static var benchmarks: BenchmarkSuite {
BenchmarkSuite(name: "UniqueID") { suite in
suite.benchmark("Random") {
for _ in 0..<NumberOfUUIDs {
blackHole(UniqueID.random())
}
}
suite.benchmark("TimeOrdered") {
for _ in 0..<NumberOfUUIDs {
blackHole(UniqueID.timeOrdered())
}
}
suite.benchmark("Random.Parallel") {
let results = Array<[UniqueID]>(unsafeUninitializedCapacity: numberOfTasks) { buffer, count in
let buffer = buffer
let group = DispatchGroup()
group.enter()
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.concurrentPerform(iterations: numberOfTasks) { taskOffset in
(buffer.baseAddress! + taskOffset).initialize(
to: (0..<uuidsPerTask).map { _ in UniqueID.random() }
)
}
group.leave()
}
group.wait()
count = numberOfTasks
}
blackHole(results)
}
suite.benchmark("TimeOrdered.Parallel") {
let results = Array<[UniqueID]>(unsafeUninitializedCapacity: numberOfTasks) { buffer, count in
let buffer = buffer
let group = DispatchGroup()
group.enter()
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.concurrentPerform(iterations: numberOfTasks) { taskOffset in
(buffer.baseAddress! + taskOffset).initialize(
to: (0..<uuidsPerTask).map { _ in UniqueID.timeOrdered() }
)
}
group.leave()
}
group.wait()
count = numberOfTasks
}
blackHole(results)
}
}
}
}
extension UUID {
static var benchmarks: BenchmarkSuite {
BenchmarkSuite(name: "Foundation.UUID") { suite in
suite.benchmark("Random") {
for _ in 0..<NumberOfUUIDs {
blackHole(UUID())
}
}
suite.benchmark("Random.Parallel") {
let results = Array<[UUID]>(unsafeUninitializedCapacity: numberOfTasks) { buffer, count in
let buffer = buffer
let group = DispatchGroup()
group.enter()
DispatchQueue.global(qos: .userInitiated).async {
DispatchQueue.concurrentPerform(iterations: numberOfTasks) { taskOffset in
(buffer.baseAddress! + taskOffset).initialize(
to: (0..<uuidsPerTask).map { _ in UUID() }
)
}
group.leave()
}
group.wait()
count = numberOfTasks
}
blackHole(results)
}
}
}
}
Benchmark.main([UniqueID.benchmarks, UUID.benchmarks])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment