Skip to content

Commit 22d7c28

Browse files
authored
Merge pull request #25310 from keitaito/string-benchmarks
[benchmark] Add ReplaceSubrange benchmark
2 parents fab8d3d + a9cbe2b commit 22d7c28

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ set(SWIFT_BENCH_MODULES
166166
single-source/StringInterpolation
167167
single-source/StringMatch
168168
single-source/StringRemoveDupes
169+
single-source/StringReplaceSubrange
169170
single-source/StringTests
170171
single-source/StringWalk
171172
single-source/Substring
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===--- StringReplaceSubrange.swift -------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
let tags: [BenchmarkCategory] = [.validation, .api, .String]
16+
17+
public let StringReplaceSubrange = [
18+
BenchmarkInfo(
19+
name: "String.replaceSubrange.String.Small",
20+
runFunction: { replaceSubrange($0, smallString, with: "t") },
21+
tags: tags
22+
),
23+
BenchmarkInfo(
24+
name: "String.replaceSubrange.String",
25+
runFunction: { replaceSubrange($0, largeString, with: "t") },
26+
tags: tags
27+
),
28+
BenchmarkInfo(
29+
name: "String.replaceSubrange.Substring.Small",
30+
runFunction: { replaceSubrange($0, smallString, with: "t"[...]) },
31+
tags: tags
32+
),
33+
BenchmarkInfo(
34+
name: "String.replaceSubrange.Substring",
35+
runFunction: { replaceSubrange($0, largeString, with: "t"[...]) },
36+
tags: tags
37+
),
38+
BenchmarkInfo(
39+
name: "String.replaceSubrange.ArrChar.Small",
40+
runFunction: { replaceSubrange($0, smallString, with: arrayCharacter) },
41+
tags: tags
42+
),
43+
BenchmarkInfo(
44+
name: "String.replaceSubrange.ArrChar",
45+
runFunction: { replaceSubrange($0, largeString, with: arrayCharacter) },
46+
tags: tags
47+
),
48+
BenchmarkInfo(
49+
name: "String.replaceSubrange.RepChar.Small",
50+
runFunction: { replaceSubrange($0, smallString, with: repeatedCharacter) },
51+
tags: tags
52+
),
53+
BenchmarkInfo(
54+
name: "String.replaceSubrange.RepChar",
55+
runFunction: { replaceSubrange($0, largeString, with: repeatedCharacter) },
56+
tags: tags
57+
),
58+
]
59+
60+
let smallString = "coffee"
61+
let largeString = "coffee\u{301}coffeecoffeecoffeecoffee"
62+
63+
let arrayCharacter = Array<Character>(["t"])
64+
let repeatedCharacter = repeatElement(Character("t"), count: 1)
65+
66+
@inline(never)
67+
private func replaceSubrange<C: Collection>(
68+
_ N: Int, _ string: String, with newElements: C
69+
) where C.Element == Character {
70+
var copy = getString(string)
71+
let range = string.startIndex..<string.index(after: string.startIndex)
72+
for _ in 0 ..< 500 * N {
73+
copy.replaceSubrange(range, with: newElements)
74+
}
75+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ import StringEnum
161161
import StringInterpolation
162162
import StringMatch
163163
import StringRemoveDupes
164+
import StringReplaceSubrange
164165
import StringTests
165166
import StringWalk
166167
import Substring
@@ -343,6 +344,7 @@ registerBenchmark(StringInterpolationManySmallSegments)
343344
registerBenchmark(StringMatch)
344345
registerBenchmark(StringNormalization)
345346
registerBenchmark(StringRemoveDupes)
347+
registerBenchmark(StringReplaceSubrange)
346348
registerBenchmark(StringTests)
347349
registerBenchmark(StringWalk)
348350
registerBenchmark(SubstringTest)

0 commit comments

Comments
 (0)