@@ -19,54 +19,94 @@ import Cxx
19
19
20
20
public let benchmarks = [
21
21
BenchmarkInfo (
22
- name: " CxxVectorOfU32SumInCxx " ,
23
- runFunction: run_CxxVectorOfU32SumInCxx,
24
- tags: [ . validation, . bridging, . cxxInterop] ,
25
- setUpFunction: create_CxxVectorOfU32) ,
22
+ name: " CxxVectorOfU32.Sum.Cxx.RangedForLoop " ,
23
+ runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop,
24
+ tags: [ . validation, . bridging, . cxxInterop] ) ,
26
25
BenchmarkInfo (
27
- name: " CxxVectorOfU32SumInSwift_Fastest " ,
28
- runFunction: run_CxxVectorOfU32SumInSwift_Fastest,
29
- tags: [ . validation, . bridging, . cxxInterop] ,
30
- setUpFunction: create_CxxVectorOfU32) ,
26
+ name: " CxxVectorOfU32.Sum.Swift.ForInLoop " ,
27
+ runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop,
28
+ tags: [ . validation, . bridging, . cxxInterop] ) ,
31
29
BenchmarkInfo (
32
- name: " CxxVectorOfU32SumInSwift " ,
33
- runFunction: run_CxxVectorOfU32SumInSwift,
34
- tags: [ . validation, . bridging, . cxxInterop] ,
35
- setUpFunction: create_CxxVectorOfU32)
30
+ name: " CxxVectorOfU32.Sum.Swift.RawIteratorLoop " ,
31
+ runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop,
32
+ tags: [ . validation, . bridging, . cxxInterop] ) ,
33
+ BenchmarkInfo (
34
+ name: " CxxVectorOfU32.Sum.Swift.IndexAndSubscriptLoop " ,
35
+ runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop,
36
+ tags: [ . validation, . bridging, . cxxInterop] ) ,
37
+ BenchmarkInfo (
38
+ name: " CxxVectorOfU32.Sum.Swift.Reduce " ,
39
+ runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce,
40
+ tags: [ . validation, . bridging, . cxxInterop] )
36
41
]
37
42
38
43
// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
39
44
// establish an expected threshold of performance, which when exceeded should
40
45
// fail the benchmark.
41
46
42
- var vectorOfU32 : VectorOfU32 !
47
+ let vectorSize = 100_000
48
+ let iterRepeatFactor = 10
43
49
44
- func create_CxxVectorOfU32( ) {
45
- vectorOfU32 = makeVector32 ( 1_000_000 )
50
+ @inline ( never)
51
+ public func run_CxxVectorOfU32_Sum_Cxx_RangedForLoop( _ n: Int ) {
52
+ let sum = testVector32Sum ( vectorSize, n * iterRepeatFactor)
53
+ blackHole ( sum)
46
54
}
47
55
48
56
@inline ( never)
49
- public func run_CxxVectorOfU32SumInCxx( _ n: Int ) {
50
- // FIXME: Use no implicitly copyable, immutable borrow instead.
51
- // https://github.com/apple/swift/issues/61454
52
- let sum = testVector32Sum ( & vectorOfU32)
57
+ public func run_CxxVectorOfU32_Sum_Swift_ForInLoop( _ n: Int ) {
58
+ let vectorOfU32 = makeVector32 ( vectorSize)
59
+ var sum : UInt32 = 0
60
+ for _ in 0 ..< ( n * iterRepeatFactor) {
61
+ for x in vectorOfU32 {
62
+ sum = sum &+ x
63
+ }
64
+ }
65
+ blackHole ( sum)
66
+ }
67
+
68
+ // This function should have comparable performance to
69
+ // `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
70
+ @inline ( never)
71
+ public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop( _ n: Int ) {
72
+ let vectorOfU32 = makeVector32 ( vectorSize)
73
+ var sum : UInt32 = 0
74
+ for _ in 0 ..< ( n * iterRepeatFactor) {
75
+ var b = vectorOfU32. __beginUnsafe ( )
76
+ let e = vectorOfU32. __endUnsafe ( )
77
+ while !cmp( b, e) {
78
+ sum = sum &+ b. pointee
79
+ b = next ( b)
80
+ }
81
+ }
53
82
blackHole ( sum)
54
83
}
55
84
56
- // This function should have comparable performance to `run_CxxVectorOfU32SumInCxx`.
57
85
@inline ( never)
58
- public func run_CxxVectorOfU32SumInSwift_Fastest( _ n: Int ) {
86
+ public func run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop( _ n: Int ) {
87
+ let vectorOfU32 = makeVector32 ( vectorSize)
59
88
var sum : UInt32 = 0
60
- // begin mutating, end mutating needed to avoid copies right now.
61
- var b = vectorOfU32. beginMutating ( )
62
- let e = vectorOfU32. endMutating ( )
63
- while !cmp( b, e) {
64
- sum = sum &+ b. pointee
65
- b = next ( b)
89
+ for _ in 0 ..< ( n * iterRepeatFactor) {
90
+ var i = 0
91
+ let e = vectorOfU32. size ( )
92
+ while i != e {
93
+ sum = sum &+ vectorOfU32 [ i]
94
+ i = i &+ 1
95
+ }
66
96
}
67
97
blackHole ( sum)
68
98
}
69
99
100
+ @inline ( never)
101
+ public func run_CxxVectorOfU32_Sum_Swift_Reduce( _ n: Int ) {
102
+ let vectorOfU32 = makeVector32 ( vectorSize)
103
+ var sum : UInt32 = 0
104
+ for _ in 0 ..< ( n * iterRepeatFactor) {
105
+ sum = vectorOfU32. reduce ( sum, &+ )
106
+ }
107
+ blackHole ( sum)
108
+ }
109
+
70
110
public func != ( _ y: VectorOfU32 . const_iterator , _ x: VectorOfU32 . const_iterator ) -> Bool {
71
111
return y. __baseUnsafe ( ) != x. __baseUnsafe ( )
72
112
}
@@ -78,12 +118,3 @@ public func ==(_ y: VectorOfU32.const_iterator, _ x: VectorOfU32.const_iterator)
78
118
extension VectorOfU32 . const_iterator : Equatable , UnsafeCxxInputIterator { }
79
119
80
120
extension VectorOfU32 : CxxSequence { }
81
-
82
- @inline ( never)
83
- public func run_CxxVectorOfU32SumInSwift( _ n: Int ) {
84
- var sum : UInt32 = 0
85
- for i in vectorOfU32 {
86
- sum = sum &+ i
87
- }
88
- blackHole ( sum)
89
- }
0 commit comments