Skip to content

Commit 1c0778b

Browse files
BalestraPatrickmilseman
authored andcommitted
[benchmark] Add insert(_:Character) benchmark with ASCII and non-ASCII characters
Adds insert character benchmark with ASCII and non-ASCII characters
1 parent bf2cf99 commit 1c0778b

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ set(SWIFT_BENCH_MODULES
8888
single-source/Hash
8989
single-source/HashQuadratic
9090
single-source/Histogram
91+
single-source/InsertCharacter
9192
single-source/Integrate
9293
single-source/IterateData
9394
single-source/Join
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
//===--- InsertCharacter.swift ------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 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 InsertCharacter = [
16+
BenchmarkInfo(name: "InsertCharacterEndIndex", runFunction: run_InsertCharacterEndIndex, tags: [.validation, .api], setUpFunction: buildWorkload),
17+
BenchmarkInfo(name: "InsertCharacterTowardsEndIndex", runFunction: run_InsertCharacterTowardsEndIndex, tags: [.validation, .api], setUpFunction: buildWorkload),
18+
BenchmarkInfo(name: "InsertCharacterStartIndex", runFunction: run_InsertCharacterStartIndex, tags: [.validation, .api], setUpFunction: buildWorkload),
19+
BenchmarkInfo(name: "InsertCharacterEndIndexNonASCII", runFunction: run_InsertCharacterEndIndexNonASCII, tags: [.validation, .api], setUpFunction: buildWorkload),
20+
BenchmarkInfo(name: "InsertCharacterTowardsEndIndexNonASCII", runFunction: run_InsertCharacterTowardsEndIndexNonASCII, tags: [.validation, .api], setUpFunction: buildWorkload),
21+
BenchmarkInfo(name: "InsertCharacterStartIndexNonASCII", runFunction: run_InsertCharacterStartIndexNonASCII, tags: [.validation, .api], setUpFunction: buildWorkload)
22+
]
23+
24+
let str = String(repeating: "A very long ASCII string.", count: 200)
25+
26+
func buildWorkload() {
27+
blackHole(str)
28+
}
29+
30+
// Insert towards end index
31+
32+
@inline(__always)
33+
func insertTowardsEndIndex(_ c: Character, in string: String, count: Int) {
34+
var workload = string
35+
var index = workload.endIndex
36+
for i in 0..<count {
37+
workload.insert(identity(c), at: index)
38+
if i % 1000 == 0 {
39+
index = workload.endIndex
40+
}
41+
}
42+
blackHole(workload)
43+
}
44+
45+
@inline(never)
46+
func run_InsertCharacterTowardsEndIndex(_ N: Int) {
47+
insertTowardsEndIndex("s", in: str, count: N * 3000)
48+
}
49+
50+
@inline(never)
51+
func run_InsertCharacterTowardsEndIndexNonASCII(_ N: Int) {
52+
insertTowardsEndIndex("👩🏼‍💻", in: str, count: N * 1000)
53+
}
54+
55+
// Insert at end index
56+
57+
@inline(__always)
58+
func insertAtEndIndex(_ c: Character, in string: String, count: Int) {
59+
var workload = string
60+
for _ in 0..<count {
61+
workload.insert(identity(c), at: workload.endIndex)
62+
}
63+
blackHole(workload)
64+
}
65+
66+
@inline(never)
67+
func run_InsertCharacterEndIndex(_ N: Int) {
68+
insertAtEndIndex("s", in: str, count: N * 3000)
69+
}
70+
71+
@inline(never)
72+
func run_InsertCharacterEndIndexNonASCII(_ N: Int) {
73+
insertAtEndIndex("👩🏾‍🏫", in: str, count: N * 1000)
74+
}
75+
76+
// Insert at start index
77+
78+
@inline(__always)
79+
func insertAtStartIndex(_ c: Character, in string: String, count: Int, insertions: Int) {
80+
var workload = str
81+
for _ in 0..<count {
82+
for _ in 0..<insertions {
83+
workload.insert(identity(c), at: workload.startIndex)
84+
}
85+
workload = str
86+
}
87+
blackHole(workload)
88+
}
89+
90+
@inline(never)
91+
func run_InsertCharacterStartIndex(_ N: Int) {
92+
insertAtStartIndex("w", in: str, count: N * 75, insertions: 50)
93+
}
94+
95+
@inline(never)
96+
func run_InsertCharacterStartIndexNonASCII(_ N: Int) {
97+
insertAtStartIndex("👩🏾‍🏫", in: str, count: N * 75, insertions: 25)
98+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import Hanoi
7676
import Hash
7777
import HashQuadratic
7878
import Histogram
79+
import InsertCharacter
7980
import Integrate
8081
import IterateData
8182
import Join
@@ -241,6 +242,7 @@ registerBenchmark(Hanoi)
241242
registerBenchmark(HashTest)
242243
registerBenchmark(HashQuadratic)
243244
registerBenchmark(Histogram)
245+
registerBenchmark(InsertCharacter)
244246
registerBenchmark(IntegrateTest)
245247
registerBenchmark(IterateData)
246248
registerBenchmark(Join)

0 commit comments

Comments
 (0)