Skip to content

Commit c792fec

Browse files
authored
Merge pull request #32492 from valeriyvan/StringRepeatingBenchmark
[stdlib][string] Add benchmark for String(repeating:count:)
2 parents ba34e4f + e99a45b commit c792fec

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//===--- StringRepeating.swift --------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 benchmarks = [
16+
BenchmarkInfo(name: "String.initRepeating.1AsciiChar.Count100",
17+
runFunction: run_singleAsciiCharacterCount100,
18+
tags: [.validation, .api, .String]),
19+
BenchmarkInfo(name: "String.initRepeating.26AsciiChar.Count2",
20+
runFunction: run_26AsciiCharactersCount2,
21+
tags: [.validation, .api, .String]),
22+
BenchmarkInfo(name: "String.initRepeating.33CyrillicChar.Count2",
23+
runFunction: run_33CyrillicCharactersCount2,
24+
tags: [.validation, .api, .String]),
25+
BenchmarkInfo(name: "String.initRepeating.longMixStr.Count100",
26+
runFunction: run_longMixedStringCount100,
27+
tags: [.validation, .api, .String])
28+
]
29+
30+
@inline(never)
31+
public func run_singleAsciiCharacterCount100(N: Int) {
32+
let string = "x"
33+
for _ in 1...200*N {
34+
blackHole(String(repeating: getString(string), count: 100))
35+
}
36+
}
37+
38+
@inline(never)
39+
public func run_26AsciiCharactersCount2(N: Int) {
40+
let string = "abcdefghijklmnopqrstuvwxyz"
41+
for _ in 1...200*N {
42+
blackHole(String(repeating: getString(string), count: 2))
43+
}
44+
}
45+
46+
@inline(never)
47+
public func run_33CyrillicCharactersCount2(N: Int) {
48+
let string = "абвгґдеєжзиіїйклмнопрстуфхцчшщьюя"
49+
for _ in 1...200*N {
50+
blackHole(String(repeating: getString(string), count: 2))
51+
}
52+
}
53+
54+
@inline(never)
55+
public func run_longMixedStringCount100(N: Int) {
56+
let string = """
57+
Swift is a multi-paradigm, compiled programming language created for
58+
iOS, OS X, watchOS, tvOS and Linux development by Apple Inc. Swift is
59+
designed to work with Apple's Cocoa and Cocoa Touch frameworks and the
60+
large body of existing Objective-C code written for Apple products. Swift
61+
is intended to be more resilient to erroneous code (\"safer\") than
62+
Objective-C and also more concise. It is built with the LLVM compiler
63+
framework included in Xcode 6 and later and uses the Objective-C runtime,
64+
which allows C, Objective-C, C++ and Swift code to run within a single
65+
program.
66+
Існує багато варіацій уривків з Lorem Ipsum, але більшість з них зазнала
67+
певних змін на кшталт жартівливих вставок або змішування слів, які навіть
68+
не виглядають правдоподібно.
69+
日本語の場合はランダムに生成された文章以外に、
70+
著作権が切れた小説などが利用されることもある。
71+
🦩
72+
"""
73+
for _ in 1...200*N {
74+
blackHole(String(repeating: getString(string), count: 100))
75+
}
76+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ import StringEnum
188188
import StringInterpolation
189189
import StringMatch
190190
import StringRemoveDupes
191+
import StringRepeating
191192
import StringReplaceSubrange
192193
import StringSplitting
193194
import StringSwitch
@@ -379,6 +380,7 @@ register(StringEnum.benchmarks)
379380
register(StringInterpolation.benchmarks)
380381
register(StringMatch.benchmarks)
381382
register(StringRemoveDupes.benchmarks)
383+
register(StringRepeating.benchmarks)
382384
register(StringReplaceSubrange.benchmarks)
383385

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

0 commit comments

Comments
 (0)