Last active
October 28, 2016 03:29
-
-
Save Cananito/1b4a91e69b568a17ebe27b99eb74b41c 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
import XCTest | |
class CopyOnWriteWorkaroundTests: XCTestCase { | |
func testFirst() { | |
self.measure { //0.272 sec | |
var dict = ["a":[]] | |
for i in 0..<10000 { | |
if dict["a"] != nil { | |
dict["a"]!.append(i) | |
} else { | |
dict["a"] = [] | |
} | |
} | |
} | |
} | |
func testSecond() { | |
self.measure { // 0.254 sec | |
var dict = ["a":[]] | |
for i in 0..<10000 { | |
var arr = dict["a"] ?? [] | |
arr.append(i) | |
dict["a"] = arr | |
} | |
} | |
} | |
func testCorrect() { | |
var dict: Dictionary<String, [Int]> = ["a":[]] | |
for i in 0..<5 { | |
if dict["a"] != nil { | |
dict["a"]!.append(i) | |
} else { | |
dict["a"] = [] | |
} | |
} | |
let arr = dict["a"]! | |
XCTAssert(arr == [0, 1, 2, 3, 4]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment