Skip to content

Commit 1c9cc38

Browse files
committed
Performance tests for Sequence methods: DropFirst, DropWhile, PrefixWhile
1 parent 739c3b7 commit 1c9cc38

File tree

8 files changed

+799
-0
lines changed

8 files changed

+799
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ set(SWIFT_BENCH_MODULES
4444
single-source/DictionaryLiteral
4545
single-source/DictionaryRemove
4646
single-source/DictionarySwap
47+
single-source/DropFirst
4748
single-source/DropLast
49+
single-source/DropWhile
4850
single-source/ErrorHandling
4951
single-source/ExistentialPerformance
5052
single-source/Fibonacci
@@ -80,6 +82,7 @@ set(SWIFT_BENCH_MODULES
8082
single-source/PopFront
8183
single-source/PopFrontGeneric
8284
single-source/Prefix
85+
single-source/PrefixWhile
8386
single-source/Prims
8487
single-source/ProtocolDispatch
8588
single-source/ProtocolDispatch2
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
//===--- DropFirst.swift ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
////////////////////////////////////////////////////////////////////////////////
14+
// WARNING: This file is manually generated from .gyb template and should not
15+
// be directly modified. Instead, make changes to DropFirst.swift.gyb and run
16+
// scripts/generate_harness/generate_harness.py to regenerate this file.
17+
////////////////////////////////////////////////////////////////////////////////
18+
19+
import TestsUtils
20+
21+
let sequenceCount = 4096
22+
let dropCount = 1024
23+
let suffixCount = sequenceCount - dropCount
24+
let sumCount = suffixCount * (2 * sequenceCount - suffixCount - 1) / 2
25+
26+
@inline(never)
27+
public func run_DropFirstCountableRange(_ N: Int) {
28+
let s = 0..<sequenceCount
29+
for _ in 1...20*N {
30+
var result = 0
31+
for element in s.dropFirst(dropCount) {
32+
result += element
33+
}
34+
CheckResults(result == sumCount,
35+
"IncorrectResults in DropFirstCountableRange: \(result) != \(sumCount)")
36+
}
37+
}
38+
@inline(never)
39+
public func run_DropFirstSequence(_ N: Int) {
40+
let s = sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil }
41+
for _ in 1...20*N {
42+
var result = 0
43+
for element in s.dropFirst(dropCount) {
44+
result += element
45+
}
46+
CheckResults(result == sumCount,
47+
"IncorrectResults in DropFirstSequence: \(result) != \(sumCount)")
48+
}
49+
}
50+
@inline(never)
51+
public func run_DropFirstAnySequence(_ N: Int) {
52+
let s = AnySequence(sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil })
53+
for _ in 1...20*N {
54+
var result = 0
55+
for element in s.dropFirst(dropCount) {
56+
result += element
57+
}
58+
CheckResults(result == sumCount,
59+
"IncorrectResults in DropFirstAnySequence: \(result) != \(sumCount)")
60+
}
61+
}
62+
@inline(never)
63+
public func run_DropFirstAnySeqCntRange(_ N: Int) {
64+
let s = AnySequence(0..<sequenceCount)
65+
for _ in 1...20*N {
66+
var result = 0
67+
for element in s.dropFirst(dropCount) {
68+
result += element
69+
}
70+
CheckResults(result == sumCount,
71+
"IncorrectResults in DropFirstAnySeqCntRange: \(result) != \(sumCount)")
72+
}
73+
}
74+
@inline(never)
75+
public func run_DropFirstAnySeqCRangeIter(_ N: Int) {
76+
let s = AnySequence((0..<sequenceCount).makeIterator())
77+
for _ in 1...20*N {
78+
var result = 0
79+
for element in s.dropFirst(dropCount) {
80+
result += element
81+
}
82+
CheckResults(result == sumCount,
83+
"IncorrectResults in DropFirstAnySeqCRangeIter: \(result) != \(sumCount)")
84+
}
85+
}
86+
@inline(never)
87+
public func run_DropFirstAnyCollection(_ N: Int) {
88+
let s = AnyCollection(0..<sequenceCount)
89+
for _ in 1...20*N {
90+
var result = 0
91+
for element in s.dropFirst(dropCount) {
92+
result += element
93+
}
94+
CheckResults(result == sumCount,
95+
"IncorrectResults in DropFirstAnyCollection: \(result) != \(sumCount)")
96+
}
97+
}
98+
@inline(never)
99+
public func run_DropFirstArray(_ N: Int) {
100+
let s = Array(0..<sequenceCount)
101+
for _ in 1...20*N {
102+
var result = 0
103+
for element in s.dropFirst(dropCount) {
104+
result += element
105+
}
106+
CheckResults(result == sumCount,
107+
"IncorrectResults in DropFirstArray: \(result) != \(sumCount)")
108+
}
109+
}
110+
@inline(never)
111+
public func run_DropFirstCountableRangeLazy(_ N: Int) {
112+
let s = (0..<sequenceCount).lazy
113+
for _ in 1...20*N {
114+
var result = 0
115+
for element in s.dropFirst(dropCount) {
116+
result += element
117+
}
118+
CheckResults(result == sumCount,
119+
"IncorrectResults in DropFirstCountableRangeLazy: \(result) != \(sumCount)")
120+
}
121+
}
122+
@inline(never)
123+
public func run_DropFirstSequenceLazy(_ N: Int) {
124+
let s = (sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil }).lazy
125+
for _ in 1...20*N {
126+
var result = 0
127+
for element in s.dropFirst(dropCount) {
128+
result += element
129+
}
130+
CheckResults(result == sumCount,
131+
"IncorrectResults in DropFirstSequenceLazy: \(result) != \(sumCount)")
132+
}
133+
}
134+
@inline(never)
135+
public func run_DropFirstAnySequenceLazy(_ N: Int) {
136+
let s = (AnySequence(sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil })).lazy
137+
for _ in 1...20*N {
138+
var result = 0
139+
for element in s.dropFirst(dropCount) {
140+
result += element
141+
}
142+
CheckResults(result == sumCount,
143+
"IncorrectResults in DropFirstAnySequenceLazy: \(result) != \(sumCount)")
144+
}
145+
}
146+
@inline(never)
147+
public func run_DropFirstAnySeqCntRangeLazy(_ N: Int) {
148+
let s = (AnySequence(0..<sequenceCount)).lazy
149+
for _ in 1...20*N {
150+
var result = 0
151+
for element in s.dropFirst(dropCount) {
152+
result += element
153+
}
154+
CheckResults(result == sumCount,
155+
"IncorrectResults in DropFirstAnySeqCntRangeLazy: \(result) != \(sumCount)")
156+
}
157+
}
158+
@inline(never)
159+
public func run_DropFirstAnySeqCRangeIterLazy(_ N: Int) {
160+
let s = (AnySequence((0..<sequenceCount).makeIterator())).lazy
161+
for _ in 1...20*N {
162+
var result = 0
163+
for element in s.dropFirst(dropCount) {
164+
result += element
165+
}
166+
CheckResults(result == sumCount,
167+
"IncorrectResults in DropFirstAnySeqCRangeIterLazy: \(result) != \(sumCount)")
168+
}
169+
}
170+
@inline(never)
171+
public func run_DropFirstAnyCollectionLazy(_ N: Int) {
172+
let s = (AnyCollection(0..<sequenceCount)).lazy
173+
for _ in 1...20*N {
174+
var result = 0
175+
for element in s.dropFirst(dropCount) {
176+
result += element
177+
}
178+
CheckResults(result == sumCount,
179+
"IncorrectResults in DropFirstAnyCollectionLazy: \(result) != \(sumCount)")
180+
}
181+
}
182+
@inline(never)
183+
public func run_DropFirstArrayLazy(_ N: Int) {
184+
let s = (Array(0..<sequenceCount)).lazy
185+
for _ in 1...20*N {
186+
var result = 0
187+
for element in s.dropFirst(dropCount) {
188+
result += element
189+
}
190+
CheckResults(result == sumCount,
191+
"IncorrectResults in DropFirstArrayLazy: \(result) != \(sumCount)")
192+
}
193+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- DropFirst.swift ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 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+
% # Ignore the following warning. This _is_ the correct file to edit.
14+
////////////////////////////////////////////////////////////////////////////////
15+
// WARNING: This file is manually generated from .gyb template and should not
16+
// be directly modified. Instead, make changes to DropFirst.swift.gyb and run
17+
// scripts/generate_harness/generate_harness.py to regenerate this file.
18+
////////////////////////////////////////////////////////////////////////////////
19+
20+
import TestsUtils
21+
22+
let sequenceCount = 4096
23+
let dropCount = 1024
24+
let suffixCount = sequenceCount - dropCount
25+
let sumCount = suffixCount * (2 * sequenceCount - suffixCount - 1) / 2
26+
%{
27+
# Name and Expression pairs for Sequences to test.
28+
29+
Sequences = [
30+
('CountableRange', '0..<sequenceCount'),
31+
('Sequence',
32+
'sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil }'),
33+
('AnySequence',
34+
'AnySequence(sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil })'),
35+
('AnySeqCntRange', 'AnySequence(0..<sequenceCount)'),
36+
('AnySeqCRangeIter', 'AnySequence((0..<sequenceCount).makeIterator())'),
37+
('AnyCollection', 'AnyCollection(0..<sequenceCount)'),
38+
('Array', 'Array(0..<sequenceCount)'),
39+
]
40+
def lazy ((Name, Expr)) : return (Name + 'Lazy', '(' + Expr + ').lazy')
41+
42+
Sequences = Sequences + map(lazy, Sequences)
43+
}%
44+
45+
% for (Name, Expr) in Sequences:
46+
@inline(never)
47+
public func run_DropFirst${Name}(_ N: Int) {
48+
let s = ${Expr}
49+
for _ in 1...20*N {
50+
var result = 0
51+
for element in s.dropFirst(dropCount) {
52+
result += element
53+
}
54+
CheckResults(result == sumCount,
55+
"IncorrectResults in DropFirst${Name}: \(result) != \(sumCount)")
56+
}
57+
}
58+
% end

0 commit comments

Comments
 (0)