Last active
January 15, 2017 06:28
-
-
Save mono0926/6839352ed11136f0d1a860e5390e2806 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 StringPerformanceTests2: XCTestCase { | |
let chars = Array("あいうえお一二三四五1️⃣2️⃣3️⃣4️⃣5️⃣".characters) | |
var input: String! | |
override func setUp() { | |
super.setUp() | |
input = ((0..<100000).map { _ in String(chars[Int(arc4random_uniform(UInt32(chars.count))) % chars.count]) }).joined(separator: "") | |
} | |
func testPerformanceBytesInitializer() { | |
let bytes = Array(input.utf8) | |
self.measure { | |
(0..<10).forEach { _ in | |
_ = String(bytes: bytes, encoding: String.Encoding.utf8) | |
} | |
} | |
} | |
func testPerformanceCharactersArrayCast() { | |
self.measure { | |
(0..<10).forEach { _ in | |
_ = Array(self.input.characters) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実機・リリースビルドにて、"あいうえお一二三四五1️⃣2️⃣3️⃣4️⃣5️⃣"からランダムに取得して組み立てた10万文字の文字列で検証。
Character Viewの全スキャンは高コストで、例えば
String.init(bytes:encoding:)
と比べて27倍程度時間かかった(文字列内容が"abcdefghijklmnopqrstuvwxyz"からランダムの時は1,000倍程度差が付いたり文字列によって結果はけっこう異なった)。