Skip to content

Commit bfb1e87

Browse files
committed
Adds benchmark for String(repeating:count:)
1 parent f89c11a commit bfb1e87

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

benchmark/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ set(SWIFT_BENCH_MODULES
187187
single-source/StringInterpolation
188188
single-source/StringMatch
189189
single-source/StringRemoveDupes
190+
single-source/StringRepeating
190191
single-source/StringReplaceSubrange
191192
single-source/StringSplitting
192193
single-source/StringSwitch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
//===--- StringRepeating.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+
public let StringRepeating = [
16+
BenchmarkInfo(name: "StringRepeating/SingleAsciiCharacterCount10",
17+
runFunction: run_singleAsciiCharacterCount10,
18+
tags: [.validation, .api, .String]),
19+
BenchmarkInfo(name: "StringRepeating/SingleAsciiCharacterCount1",
20+
runFunction: run_singleAsciiCharacterCount1,
21+
tags: [.validation, .api, .String]),
22+
BenchmarkInfo(name: "StringRepeating/EmptyStringCount10",
23+
runFunction: run_emptyStringCount10,
24+
tags: [.validation, .api, .String]),
25+
BenchmarkInfo(name: "StringRepeating/SingleAsciiCharacterCount0",
26+
runFunction: run_singleAsciiCharacterCount0,
27+
tags: [.validation, .api, .String]),
28+
BenchmarkInfo(name: "StringRepeating/26AsciiCharactersCount2",
29+
runFunction: run_26AsciiCharactersCount2,
30+
tags: [.validation, .api, .String]),
31+
BenchmarkInfo(name: "StringRepeating/SingleCyrilicCharacterCount5",
32+
runFunction: run_singleCyrilicCharacterCount5,
33+
tags: [.validation, .api, .String]),
34+
BenchmarkInfo(name: "StringRepeating/33CyrilicCharactersCount2",
35+
runFunction: run_33CyrilicCharactersCount2,
36+
tags: [.validation, .api, .String]),
37+
BenchmarkInfo(name: "StringRepeating/U1F1F8U1F1FACount2",
38+
runFunction: run_U1F1F8U1F1FACount2,
39+
tags: [.validation, .api, .String]),
40+
BenchmarkInfo(name: "StringRepeating/U301cafeCount5",
41+
runFunction: run_U301cafeCount5,
42+
tags: [.validation, .api, .String]),
43+
BenchmarkInfo(name: "StringRepeating/LongMixedStringCount100",
44+
runFunction: run_longMixedStringCount100,
45+
tags: [.validation, .api, .String])
46+
]
47+
48+
@inline(never)
49+
func repeating(_ i: String, count: Int) -> String {
50+
let s = String(repeating: getString(i), count: count)
51+
return s
52+
}
53+
54+
@inline(never)
55+
public func run_singleAsciiCharacterCount10(N: Int) {
56+
for _ in 1...5000*N {
57+
blackHole(repeating("x", count: 10))
58+
}
59+
}
60+
61+
@inline(never)
62+
public func run_singleAsciiCharacterCount1(N: Int) {
63+
for _ in 1...5000*N {
64+
blackHole(repeating("x", count: 1))
65+
}
66+
}
67+
68+
@inline(never)
69+
public func run_emptyStringCount10(N: Int) {
70+
for _ in 1...5000*N {
71+
blackHole(repeating("", count: 10))
72+
}
73+
}
74+
75+
@inline(never)
76+
public func run_singleAsciiCharacterCount0(N: Int) {
77+
for _ in 1...5000*N {
78+
blackHole(repeating("x", count: 0))
79+
}
80+
}
81+
82+
@inline(never)
83+
public func run_26AsciiCharactersCount2(N: Int) {
84+
for _ in 1...5000*N {
85+
blackHole(repeating("abcdefghijklmnopqrstuvwxyz", count: 2))
86+
}
87+
}
88+
89+
@inline(never)
90+
public func run_singleCyrilicCharacterCount5(N: Int) {
91+
for _ in 1...5000*N {
92+
blackHole(repeating("я", count: 5))
93+
}
94+
}
95+
96+
@inline(never)
97+
public func run_33CyrilicCharactersCount2(N: Int) {
98+
for _ in 1...5000*N {
99+
blackHole(repeating("абвгґдеєжзиіїйклмнопрстуфхцчшщьюя", count: 2))
100+
}
101+
}
102+
103+
@inline(never)
104+
public func run_U1F1F8U1F1FACount2(N: Int) {
105+
for _ in 1...5000*N {
106+
blackHole(repeating("\u{1F1F8}\u{1F1FA}\u{1F1F8}\u{1F1FA}", count: 2))
107+
}
108+
}
109+
110+
@inline(never)
111+
public func run_U301cafeCount5(N: Int) {
112+
for _ in 1...5000*N {
113+
blackHole(repeating("\u{301}cafe", count: 5))
114+
}
115+
}
116+
117+
func getLongString() -> String {
118+
let long = """
119+
Swift is a multi-paradigm, compiled programming language created for
120+
iOS, OS X, watchOS, tvOS and Linux development by Apple Inc. Swift is
121+
designed to work with Apple's Cocoa and Cocoa Touch frameworks and the
122+
large body of existing Objective-C code written for Apple products. Swift
123+
is intended to be more resilient to erroneous code (\"safer\") than
124+
Objective-C and also more concise. It is built with the LLVM compiler
125+
framework included in Xcode 6 and later and uses the Objective-C runtime,
126+
which allows C, Objective-C, C++ and Swift code to run within a single
127+
program.
128+
Існує багато варіацій уривків з Lorem Ipsum, але більшість з них зазнала
129+
певних змін на кшталт жартівливих вставок або змішування слів, які навіть
130+
не виглядають правдоподібно.
131+
日本語の場合はランダムに生成された文章以外に、
132+
著作権が切れた小説などが利用されることもある。
133+
🦩
134+
"""
135+
return getString(long)
136+
}
137+
138+
@inline(never)
139+
public func run_longMixedStringCount100(N: Int) {
140+
for _ in 1...5000*N {
141+
blackHole(repeating(getLongString(), count: 100))
142+
}
143+
}

benchmark/utils/main.swift

+1
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,7 @@ register(StringEnum.benchmarks)
379379
register(StringInterpolation.benchmarks)
380380
register(StringMatch.benchmarks)
381381
register(StringRemoveDupes.benchmarks)
382+
registerBenchmark(StringRepeating.benchmarks)
382383
register(StringReplaceSubrange.benchmarks)
383384

384385
if #available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) {

0 commit comments

Comments
 (0)