Skip to content

Instantly share code, notes, and snippets.

@LucianoPAlmeida
Created March 14, 2021 18:13
Show Gist options
  • Save LucianoPAlmeida/64cb16bda9b3d1f753cfb9e96eaedf16 to your computer and use it in GitHub Desktop.
Save LucianoPAlmeida/64cb16bda9b3d1f753cfb9e96eaedf16 to your computer and use it in GitHub Desktop.
class MyBenchmark: AnyBenchmark {
let name: String
let settings: [BenchmarkSetting]
let closure: () throws -> Void
let tearDownClosure: () -> Void
init(_ name: String, settings: [BenchmarkSetting],
closure: @escaping () throws -> Void,
tearDownClosure: @escaping () -> Void) {
self.name = name
self.settings = settings
self.closure = closure
self.tearDownClosure = tearDownClosure
}
func run(_ state: inout BenchmarkState) throws {
return try self.closure()
}
func setUp() {}
func tearDown() { tearDownClosure() }
}
let count = 1_000_000
var a = ContiguousArray<SIMD4<Int8>>(repeating: [1, 8, 5, 6], count: count)
var b = ContiguousArray<Vec4>(repeating: (1, 8, 5, 6), count: count)
let vecTupleBench = MyBenchmark("vecTupleArray", settings: [Iterations(1000)],
closure: {
vecAdd(x: &b, s: 5)
}, tearDownClosure: {
// Decrement to avoid overflow on iterations since we using the same buffer.
vecAdd(x: &b, s: -5)
})
defaultBenchmarkSuite.register(benchmark: vecTupleBench)
let vec3Bench = MyBenchmark("vecSIMDArray", settings: [Iterations(1000)],
closure: {
vecAddSIMD(x: &a, s: 5)
}, tearDownClosure: {
// Decrement to avoid overflow on iterations since we using the same buffer.
vecAddSIMD(x: &a, s: -5)
})
defaultBenchmarkSuite.register(benchmark: vec3Bench)
Benchmark.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment