Skip to content

Instantly share code, notes, and snippets.

@icanzilb
Created July 23, 2014 13:25
Show Gist options
  • Save icanzilb/864092cf0eda8709c8a5 to your computer and use it in GitHub Desktop.
Save icanzilb/864092cf0eda8709c8a5 to your computer and use it in GitHub Desktop.
Performance test for swapping values with tuples and "old way". Tuples measure constantly better results
import UIKit
import XCTest
class SwapOptimizedTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
XCTAssert(true, "Pass")
}
func testPerformanceTuplesWay() {
self.measureBlock() {
var a = "Marin"
var b = "Todorov"
for i in 1...1000000 {
self.swapValues(&a, b: &b)
}
}
}
func testPerformanceOldWay() {
self.measureBlock() {
var a = "Marin"
var b = "Todorov"
for i in 1...1000000 {
self.swapValuesOldWay(&a, b: &b)
}
}
}
func swapValues(inout a: String, inout b: String) {
(a, b) = (b, a)
}
func swapValuesOldWay(inout a: String, inout b: String) {
let c = a
a = b
b = c
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment