Created
July 23, 2014 13:25
-
-
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
This file contains hidden or 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
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