Skip to content

Instantly share code, notes, and snippets.

@mzaks
Last active February 10, 2016 23:02
Show Gist options
  • Select an option

  • Save mzaks/067e02cad80b83fe8a33 to your computer and use it in GitHub Desktop.

Select an option

Save mzaks/067e02cad80b83fe8a33 to your computer and use it in GitHub Desktop.
func testStringToByteArray1(){
let value : StaticString = "maxim"
let data = UnsafeMutablePointer<UInt8>.alloc(500_000)
var cursor = 0
let time1 = NSDate()
for _ in 0...100_000 {
let buf = value.utf8Start
let length = value.byteSize
data.advancedBy(cursor).initializeFrom(UnsafeMutablePointer<UInt8>(buf), count: length)
cursor += length
}
let after1 = NSDate()
print("\((after1.timeIntervalSince1970 - time1.timeIntervalSince1970) * 1000.0) mseconds for StaticString to binary")
}
testStringToByteArray1()
func testStringToByteArray2(){
let value = "maxim"
let data = UnsafeMutablePointer<UInt8>.alloc(500_000)
var cursor = 0
let time1 = NSDate()
for _ in 0...100_000 {
let buf = [UInt8](value.utf8)
let length = buf.count
data.advancedBy(cursor).initializeFrom(UnsafeMutablePointer<UInt8>(buf), count: length)
cursor += length
}
let after1 = NSDate()
print("\((after1.timeIntervalSince1970 - time1.timeIntervalSince1970) * 1000.0) mseconds for String through utf8 to binary")
}
testStringToByteArray2()
func testStringToByteArray3(){
let value = "maxim"
let data = UnsafeMutablePointer<UInt8>.alloc(500_000)
var cursor = 0
let time1 = NSDate()
for _ in 0...100_000 {
let buf = value.cStringUsingEncoding(NSUTF8StringEncoding)!
let length = buf.count - 1
data.advancedBy(cursor).initializeFrom(UnsafeMutablePointer<UInt8>(buf), count: length)
cursor += length
}
let after1 = NSDate()
print("\((after1.timeIntervalSince1970 - time1.timeIntervalSince1970) * 1000.0) mseconds for String through c-String to binary")
}
testStringToByteArray3()
0.271081924438477 mseconds for StaticString to binary
17.2080993652344 mseconds for String through utf8 to binary
60.7211589813232 mseconds for String through c-String to binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment