Skip to content

Instantly share code, notes, and snippets.

@nicklockwood
Last active May 4, 2021 04:47
Show Gist options
  • Save nicklockwood/9a5b846f9535c1f1e5743429a9ccd71f to your computer and use it in GitHub Desktop.
Save nicklockwood/9a5b846f9535c1f1e5743429a9ccd71f to your computer and use it in GitHub Desktop.
string concat benchmarks
import XCTest
let count = 50000
class StringsTestTests: XCTestCase {
// MARK: Copying
func testInterpolation() {
var foo = ""
measure {
foo = "foo"
for _ in 0 ..< count {
foo = "\(foo)bar"
}
}
print(foo)
}
func testPlusOperator() {
var foo = ""
measure {
foo = "foo"
for _ in 0 ..< count {
foo = foo + "bar"
}
}
print(foo)
}
func testAppending() {
var foo = ""
measure {
foo = "foo"
for _ in 0 ..< count {
foo = foo.appending("bar")
}
}
print(foo)
}
// MARK: Mutating
func testPlusEquals() {
var foo = ""
measure {
foo = "foo"
for _ in 0 ..< count {
foo += "bar"
}
}
print(foo)
}
func testAppend() {
var foo = ""
measure {
foo = "foo"
for _ in 0 ..< count {
foo.append("bar")
}
}
print(foo)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment