Created
March 30, 2018 16:14
-
-
Save rnapier/44fe9a30e7b2ddc5723183843dc64be7 to your computer and use it in GitHub Desktop.
vvsqrtf performance tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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