Created
July 4, 2016 19:55
-
-
Save Danappelxx/45ef294c6aa3dc78c53a3583b811954f to your computer and use it in GitHub Desktop.
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
// 2.2 (xcode 7.3), built with optimizations | |
final class ArrayPerfTests: XCTestCase { | |
let source = 0..<300_000 | |
// otherwise, compiler would be able to optimize these loops by inlining. | |
var map: (Int) -> String = { i in "\(i)" } | |
// 118 ms (15% STDEV) | |
func testRepeatedValue() { | |
measureBlock { | |
var array = Array(count: self.source.count, repeatedValue: "") | |
for i in self.source { | |
array[i] = self.map(i) | |
} | |
} | |
} | |
// 129 ms (22% STDEV) | |
func testForLoop() { | |
measureBlock { | |
var array = [String]() | |
array.reserveCapacity(self.source.count) | |
for i in self.source { | |
array.append(self.map(i)) | |
} | |
} | |
} | |
// 116 ms (4% STDEV) | |
func testMap() { | |
measureBlock { | |
let array = self.source.map(self.map) | |
} | |
} | |
// 85 ms (5% STDEV) | |
func testPointer() { | |
measureBlock { | |
let pointer = UnsafeMutablePointer<String>.alloc(self.source.count) | |
for i in self.source { | |
pointer.advancedBy(i).initialize(self.map(i)) | |
} | |
let array = Array(UnsafeBufferPointer(start: pointer, count: self.source.count)) | |
} | |
} | |
} |
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
// 3.0-preview-1 (xcode 8 beta 1), built with optimizations | |
final class ArrayPerfTests: XCTestCase { | |
let source = 0..<300_000 | |
// otherwise, compiler would be able to optimize these loops by inlining. | |
var map: (Int) -> String = { i in "\(i)" } | |
// 120 ms (3% STDEV) | |
func testRepeatedValue() { | |
measure { | |
var array = Array(repeating: "", count: self.source.count) | |
for i in self.source { | |
array[i] = self.map(i) | |
} | |
} | |
} | |
// 124 ms (4% STDEV) | |
func testForLoop() { | |
measure { | |
var array = [String]() | |
array.reserveCapacity(self.source.count) | |
for i in self.source { | |
array.append(self.map(i)) | |
} | |
} | |
} | |
// 122 ms (3% STDEV) | |
func testMap() { | |
measure { | |
let array = self.source.map(self.map) | |
} | |
} | |
// 79 ms (3% STDEV) | |
func testPointer() { | |
measure { | |
let pointer = UnsafeMutablePointer<String>.init(allocatingCapacity: self.source.count) | |
for i in self.source { | |
pointer.advanced(by: i).initialize(with: self.map(i)) | |
} | |
let array = Array(UnsafeBufferPointer(start: pointer, count: self.source.count)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment