gpt4 book ai didi

swift - 字符串插值和字符串连接之间的区别

转载 作者:搜寻专家 更新时间:2023-11-01 07:14:39 25 4
gpt4 key购买 nike

在专门处理非可选的 String 值时,String 插值和 String 连接之间的区别是什么?

struct MyModel {
let value1: String
let value2: String
var displayNameByConcatenation: String {
return value1 + "-" + value2
}
var displayNameByInterpolation: String {
return "\(value1)-\(value2)"
}
}
  • 是否会出现 displayNameByConcatenationdisplayNameByInterpolation 不同的情况?比如长 unicode 字符串?
  • 是否有可能以某种方式覆盖运算符 + 的行为或插值的行为,使它们在上面的示例中有所不同?
  • 一个比另一个快/慢吗?

请注意,来自 this question我们了解到字符串插值将使用 CustomStringConvertible 的 description。但是 String 连接(运算符 +)是否也调用了 description

最佳答案

从速度的角度,区分拼接(value1 + "-"+ value2)和插值("\(value1)-\(value2)" ),结果可能取决于为获得最终字符串所做的操作次数。

我在 iPhone 8 上的结果显示:

  • 如果大约有 < 30 个子串连接在一起,那么连接速度会更快
  • 如果大约有 > 30 个子串连接在一起,则插值速度更快

感谢 Sirens 发现一个并不总是比另一个更快!

亲自尝试(不要忘记根据您的需要调整经过测试的字符集和迭代):

import UIKit

class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

DispatchQueue.global(qos: .default).async {
ViewController.buildDataAndTest()
}
}

private static func buildDataAndTest(times: Int = 1_000) {
let characterSet = CharacterSet.alphanumerics
characterSet.cacheAllCharacters()
let data: [(String, String)] = (0 ..< times).map { _ in
(characterSet.randomString(length: 50), characterSet.randomString(length: 20))
}
_ = testCIA(data)
_ = testInterpol(data)
print("concatenation: " + String(resultConcatenation))
print("interpolation: \(resultInterpolation)")
}

/// concatenation in array
static var resultConcatenation: CFTimeInterval = 0
private static func testCIA(_ array: [(String, String)]) -> String {
var foo = ""
let start = CACurrentMediaTime()
for (a, b) in array {
foo = foo + " " + a + "+" + b
}
resultConcatenation = CACurrentMediaTime() - start
return foo
}

/// interpolation
static var resultInterpolation: CFTimeInterval = 0
private static func testInterpol(_ array: [(String, String)]) -> String {
var foo = ""
let start = CACurrentMediaTime()
for (a, b) in array {
foo = "\(foo) \(a)+\(b)"
}
resultInterpolation = CACurrentMediaTime() - start
return foo
}
}

extension CharacterSet {
static var cachedCharacters: [Character] = []

public func cacheAllCharacters() {
CharacterSet.cachedCharacters = characters()
}

/// extracting characters
/// https://stackoverflow.com/a/52133647/1033581
public func characters() -> [Character] {
return codePoints().compactMap { UnicodeScalar($0) }.map { Character($0) }
}
public func codePoints() -> [Int] {
var result: [Int] = []
var plane = 0
for (i, w) in bitmapRepresentation.enumerated() {
let k = i % 8193
if k == 8192 {
plane = Int(w) << 13
continue
}
let base = (plane + k) << 3
for j in 0 ..< 8 where w & 1 << j != 0 {
result.append(base + j)
}
}
return result
}

// http://stackoverflow.com/a/42895178/1033581
public func randomString(length: Int) -> String {
let charArray = CharacterSet.cachedCharacters
let charArrayCount = UInt32(charArray.count)
var randomString = ""
for _ in 0 ..< length {
randomString += String(charArray[Int(arc4random_uniform(charArrayCount))])
}
return randomString
}
}

关于swift - 字符串插值和字符串连接之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849545/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com