Skip to content

Instantly share code, notes, and snippets.

@AlexJWayne
Last active March 20, 2016 00:14
Show Gist options
  • Save AlexJWayne/328a4ca72e0db22793ed to your computer and use it in GitHub Desktop.
Save AlexJWayne/328a4ca72e0db22793ed to your computer and use it in GitHub Desktop.
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")
}
}
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