Last active
March 20, 2016 00:14
-
-
Save AlexJWayne/328a4ca72e0db22793ed 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 UIKit | |
// test harness | |
func timeBlock(block: () -> Void) { | |
let start = clock() | |
block() | |
let diff = clock() - start | |
let msec = diff * 1000 / UInt(CLOCKS_PER_SEC) | |
print("Time taken: \(msec) milliseconds") | |
} | |
// Input string | |
let input:String = "0101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101" | |
print("input character count: \(input.characters.count)") | |
// test 1 | |
timeBlock { | |
let inputBytes = input.nulTerminatedUTF8 | |
var output = Array<Bool>(count: inputBytes.count - 1, repeatedValue: false) | |
for (i, val) in inputBytes.enumerate() { | |
if val == 49 { | |
output[i] = true | |
} | |
} | |
} | |
// test 2 | |
timeBlock { | |
var output = [Bool]() | |
output.reserveCapacity(input.characters.count) | |
for char in input.characters { | |
output.append(char == "1") | |
} | |
} |
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
input character count: 1024 | |
Time taken: 106 milliseconds | |
Time taken: 3966 milliseconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment