Last active
May 4, 2021 04:47
-
-
Save nicklockwood/9a5b846f9535c1f1e5743429a9ccd71f to your computer and use it in GitHub Desktop.
string concat benchmarks
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 | |
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