-
-
Save honood/00c7f804217fb3516e9458eb34532ac1 to your computer and use it in GitHub Desktop.
string concat benchmarks
This file contains hidden or 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 | |
// 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