Skip to content

Instantly share code, notes, and snippets.

@honood
Forked from nicklockwood/StringsTest.swift
Last active March 10, 2018 10:09
Show Gist options
  • Save honood/00c7f804217fb3516e9458eb34532ac1 to your computer and use it in GitHub Desktop.
Save honood/00c7f804217fb3516e9458eb34532ac1 to your computer and use it in GitHub Desktop.
string concat benchmarks
import XCTest
// Tweet link: https://twitter.com/nicklockwood/status/972215130825154561
let count = 50000
class StringsTestTests: XCTestCase {
// Tweet link: https://twitter.com/cocoaphony/status/972227239914942474
func testNoMutate() {
var foo = ""
measure {
foo = "foo" + repeatElement("bar", count: count).joined()
}
print(foo)
}
// 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