Skip to content

Instantly share code, notes, and snippets.

@rnapier
Created March 30, 2018 16:14
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 rnapier/44fe9a30e7b2ddc5723183843dc64be7 to your computer and use it in GitHub Desktop.
Save rnapier/44fe9a30e7b2ddc5723183843dc64be7 to your computer and use it in GitHub Desktop.
vvsqrtf performance tests
// swift -O main.swift
import Foundation
import Accelerate
let N = 100_000_000
let x = (0...N).map { _ in Float(drand48() * 1_000_000) }
func test(_ f: () -> [Float], name: String) {
let start = CFAbsoluteTimeGetCurrent()
let y = f()
let end = CFAbsoluteTimeGetCurrent()
print(name, end - start, y.last!)
}
func testMap() -> [Float] {
return x.map { sqrtf($0) }
}
func testVV() -> [Float] {
var n = Int32(x.count)
var y = [Float](repeating: 0, count: x.count)
vvsqrtf(&y, x, &n)
return y
}
func testForIn() -> [Float] {
var y: [Float] = []
for val in x {
y.append(sqrtf(val))
}
return y
}
test(testMap, name: "map")
test(testVV, name: "vv ")
test(testForIn, name: "for")
// As a unit test; don't forget to set the scheme to test in Release
import XCTest
import Accelerate
class testTests: XCTestCase {
var x: [Float] = []
override func setUp() {
super.setUp()
for _ in 0...10_000_000 {
x.append(Float(drand48() * 1_000_000))
}
}
func testMap() {
var y: [Float] = []
self.measure {
y = x.map { sqrtf($0) }
}
print(y.last!)
}
func testPerformanceVV() {
var y: [Float] = []
self.measure {
var n = Int32(x.count)
y = [Float](repeating: 0, count: x.count)
vvsqrtf(&y, x, &n)
}
print(y.last!)
}
func testPerformanceForIn() {
var y: [Float] = []
self.measure {
for val in x {
y.append(sqrtf(val))
}
}
print(y.last!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment