1
- //===--- ArrayAppend .swift -- ----------------------------------------------===//
1
+ //===--- SequenceAlgos .swift ----------------------------------------------===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
@@ -18,13 +18,34 @@ import TestsUtils
18
18
// To avoid too many little micro benchmarks, it measures them all together
19
19
// for each sequence type.
20
20
21
+ let t : [ BenchmarkCategory ] = [ . validation, . api]
22
+
21
23
public let SequenceAlgos = [
22
- BenchmarkInfo ( name: " SequenceAlgosList " , runFunction: run_SequenceAlgosList, tags: [ . validation, . api] , setUpFunction: { buildWorkload ( ) } , tearDownFunction: nil ) ,
23
- BenchmarkInfo ( name: " SequenceAlgosArray " , runFunction: run_SequenceAlgosArray, tags: [ . validation, . api] , setUpFunction: { buildWorkload ( ) } , tearDownFunction: nil ) ,
24
- BenchmarkInfo ( name: " SequenceAlgosContiguousArray " , runFunction: run_SequenceAlgosContiguousArray, tags: [ . validation, . api] , setUpFunction: { buildWorkload ( ) } , tearDownFunction: nil ) ,
25
- BenchmarkInfo ( name: " SequenceAlgosRange " , runFunction: run_SequenceAlgosRange, tags: [ . validation, . api] , setUpFunction: { buildWorkload ( ) } , tearDownFunction: nil ) ,
26
- BenchmarkInfo ( name: " SequenceAlgosUnfoldSequence " , runFunction: run_SequenceAlgosUnfoldSequence, tags: [ . validation, . api] , setUpFunction: { buildWorkload ( ) } , tearDownFunction: nil ) ,
27
- BenchmarkInfo ( name: " SequenceAlgosAnySequence " , runFunction: run_SequenceAlgosAnySequence, tags: [ . validation, . api] , setUpFunction: { buildWorkload ( ) } , tearDownFunction: nil ) ,
24
+ BenchmarkInfo ( name: " SequenceAlgosList " , runFunction: { for _ in 0 ..< $0 {
25
+ benchmarkSequenceAlgos ( s: l, n: n)
26
+ benchmarkEquatableSequenceAlgos ( s: l, n: n)
27
+ } } , tags: t, setUpFunction: { blackHole ( l) } , legacyFactor: 10 ) ,
28
+ BenchmarkInfo ( name: " SequenceAlgosArray " , runFunction: { for _ in 0 ..< $0 {
29
+ benchmarkSequenceAlgos ( s: a, n: a. count)
30
+ benchmarkEquatableSequenceAlgos ( s: a, n: a. count)
31
+ } } , tags: t, setUpFunction: { blackHole ( a) } , legacyFactor: 10 ) ,
32
+ BenchmarkInfo ( name: " SequenceAlgosContiguousArray " ,
33
+ runFunction: { for _ in 0 ..< $0 {
34
+ benchmarkSequenceAlgos ( s: c, n: c. count)
35
+ benchmarkEquatableSequenceAlgos ( s: c, n: c. count)
36
+ } } , tags: t, setUpFunction: { blackHole ( c) } , legacyFactor: 10 ) ,
37
+ BenchmarkInfo ( name: " SequenceAlgosRange " , runFunction: { for _ in 0 ..< $0 {
38
+ benchmarkSequenceAlgos ( s: r, n: r. count)
39
+ benchmarkEquatableSequenceAlgos ( s: r, n: r. count)
40
+ } } , tags: t, legacyFactor: 10 ) ,
41
+ BenchmarkInfo ( name: " SequenceAlgosUnfoldSequence " ,
42
+ runFunction: { for _ in 0 ..< $0 {
43
+ benchmarkSequenceAlgos ( s: s, n: n)
44
+ } } , tags: t, setUpFunction: { blackHole ( s) } , legacyFactor: 10 ) ,
45
+ BenchmarkInfo ( name: " SequenceAlgosAnySequence " ,
46
+ runFunction: { for _ in 0 ..< $0 {
47
+ benchmarkSequenceAlgos ( s: y, n: n/ 10 )
48
+ } } , tags: t, setUpFunction: { blackHole ( y) } , legacyFactor: 100 ) ,
28
49
]
29
50
30
51
extension List : Sequence {
@@ -55,79 +76,25 @@ func benchmarkSequenceAlgos<S: Sequence>(s: S, n: Int) where S.Element == Int {
55
76
CheckResults ( s. starts ( with: s) )
56
77
}
57
78
58
- let n = 10_000
79
+ let n = 1_000
59
80
let r = 0 ..< ( n*100)
60
81
let l = List ( 0 ..< n)
61
82
let c = ContiguousArray ( 0 ..< ( n*100) )
62
83
let a = Array ( 0 ..< ( n*100) )
63
- let y = AnySequence ( 0 ..< n)
84
+ let y = AnySequence ( 0 ..< n/ 10 )
64
85
let s = sequence ( first: 0 , next: { $0 < n&- 1 ? $0&+ 1 : nil } )
65
86
66
- func buildWorkload( ) {
67
- blackHole ( l. makeIterator ( ) )
68
- blackHole ( c. makeIterator ( ) )
69
- blackHole ( a. makeIterator ( ) )
70
- blackHole ( y. makeIterator ( ) )
71
- blackHole ( s. makeIterator ( ) )
72
- }
73
-
74
- func benchmarkEquatableSequenceAlgos< S: Sequence > ( s: S , n: Int ) where S. Element == Int , S: Equatable {
87
+ func benchmarkEquatableSequenceAlgos< S: Sequence > ( s: S , n: Int )
88
+ where S. Element == Int , S: Equatable {
75
89
CheckResults ( repeatElement ( s, count: 1 ) . contains ( s) )
76
90
CheckResults ( !repeatElement( s, count: 1 ) . contains { $0 != s } )
77
91
}
78
92
79
- @inline ( never)
80
- public func run_SequenceAlgosRange( _ N: Int ) {
81
- for _ in 0 ..< N {
82
- benchmarkSequenceAlgos ( s: r, n: r. count)
83
- benchmarkEquatableSequenceAlgos ( s: r, n: r. count)
84
- }
85
- }
86
-
87
- @inline ( never)
88
- public func run_SequenceAlgosArray( _ N: Int ) {
89
- for _ in 0 ..< N {
90
- benchmarkSequenceAlgos ( s: a, n: a. count)
91
- benchmarkEquatableSequenceAlgos ( s: a, n: a. count)
92
- }
93
- }
94
-
95
- @inline ( never)
96
- public func run_SequenceAlgosContiguousArray( _ N: Int ) {
97
- for _ in 0 ..< N {
98
- benchmarkSequenceAlgos ( s: c, n: c. count)
99
- benchmarkEquatableSequenceAlgos ( s: c, n: c. count)
100
- }
101
- }
102
-
103
- @inline ( never)
104
- public func run_SequenceAlgosAnySequence( _ N: Int ) {
105
- for _ in 0 ..< N {
106
- benchmarkSequenceAlgos ( s: y, n: n)
107
- }
108
- }
109
-
110
- @inline ( never)
111
- public func run_SequenceAlgosUnfoldSequence( _ N: Int ) {
112
- for _ in 0 ..< N {
113
- benchmarkSequenceAlgos ( s: s, n: n)
114
- }
115
- }
116
-
117
- @inline ( never)
118
- public func run_SequenceAlgosList( _ N: Int ) {
119
- for _ in 0 ..< N {
120
- benchmarkSequenceAlgos ( s: l, n: n)
121
- benchmarkEquatableSequenceAlgos ( s: l, n: n)
122
- }
123
- }
124
-
125
93
enum List < Element> {
126
94
case end
127
95
indirect case node( Element , List < Element > )
128
-
96
+
129
97
init < S: BidirectionalCollection > ( _ elements: S ) where S. Element == Element {
130
98
self = elements. reversed ( ) . reduce ( . end) { . node( $1, $0) }
131
99
}
132
100
}
133
-
0 commit comments