13
13
import Benchmark
14
14
import func Benchmark. blackHole
15
15
16
- #if FOUNDATION_FRAMEWORK
17
- import Foundation
18
- #else
16
+ #if os(macOS) && USE_PACKAGE
19
17
import FoundationEssentials
20
18
import FoundationInternationalization
19
+ #else
20
+ import Foundation
21
21
#endif
22
22
23
23
let benchmarks = {
24
24
Benchmark . defaultConfiguration. maxIterations = 1_000
25
25
Benchmark . defaultConfiguration. maxDuration = . seconds( 3 )
26
26
Benchmark . defaultConfiguration. scalingFactor = . kilo
27
- Benchmark . defaultConfiguration. metrics = [ . cpuTotal, . wallClock , . mallocCountTotal, . throughput]
27
+ Benchmark . defaultConfiguration. metrics = [ . cpuTotal, . mallocCountTotal, . throughput]
28
28
29
29
let thanksgivingComponents = DateComponents ( month: 11 , weekday: 5 , weekdayOrdinal: 4 )
30
30
let cal = Calendar ( identifier: . gregorian)
@@ -42,6 +42,7 @@ let benchmarks = {
42
42
}
43
43
}
44
44
}
45
+
45
46
Benchmark ( " nextThousandThanksgivings " ) { benchmark in
46
47
var count = 1000
47
48
cal. enumerateDates ( startingAfter: thanksgivingStart, matching: thanksgivingComponents, matchingPolicy: . nextTime) { result, exactMatch, stop in
@@ -51,26 +52,34 @@ let benchmarks = {
51
52
}
52
53
}
53
54
}
54
- Benchmark ( " nextThousandThanksgivingsSequence " ) { benchmark in
55
- var count = 1000
56
- for _ in cal. dates ( byMatching: thanksgivingComponents, startingAt: thanksgivingStart, matchingPolicy: . nextTime) {
57
- count -= 1
58
- if count == 0 {
59
- break
55
+
56
+ // Only available in Swift 6 for non-Darwin platforms, macOS 15 for Darwin
57
+ #if swift(>=6.0)
58
+ if #available( macOS 15 , * ) {
59
+ Benchmark ( " nextThousandThanksgivingsSequence " ) { benchmark in
60
+ var count = 1000
61
+ for _ in cal. dates ( byMatching: thanksgivingComponents, startingAt: thanksgivingStart, matchingPolicy: . nextTime) {
62
+ count -= 1
63
+ if count == 0 {
64
+ break
65
+ }
60
66
}
61
67
}
62
- }
63
- Benchmark ( " nextThousandThanksgivingsUsingRecurrenceRule " ) { benchmark in
64
- var rule = Calendar . RecurrenceRule ( calendar: cal, frequency: . yearly, end: . afterOccurrences( 1000 ) )
65
- rule. months = [ 11 ]
66
- rule. weekdays = [ . nth( 4 , . thursday) ]
67
- rule. matchingPolicy = . nextTime
68
- var count = 0
69
- for _ in rule. recurrences ( of: thanksgivingStart) {
70
- count += 1
68
+
69
+ Benchmark ( " nextThousandThanksgivingsUsingRecurrenceRule " ) { benchmark in
70
+ var rule = Calendar . RecurrenceRule ( calendar: cal, frequency: . yearly, end: . afterOccurrences( 1000 ) )
71
+ rule. months = [ 11 ]
72
+ rule. weekdays = [ . nth( 4 , . thursday) ]
73
+ rule. matchingPolicy = . nextTime
74
+ var count = 0
75
+ for _ in rule. recurrences ( of: thanksgivingStart) {
76
+ count += 1
77
+ }
78
+ assert ( count == 1000 )
71
79
}
72
- assert ( count == 1000 )
73
- }
80
+ } // #available(macOS 15, *)
81
+ #endif // swift(>=6.0)
82
+
74
83
Benchmark ( " CurrentDateComponentsFromThanksgivings " ) { benchmark in
75
84
var count = 1000
76
85
currentCalendar. enumerateDates ( startingAfter: thanksgivingStart, matching: thanksgivingComponents, matchingPolicy: . nextTime) { result, exactMatch, stop in
@@ -82,9 +91,17 @@ let benchmarks = {
82
91
}
83
92
}
84
93
94
+ // MARK: - Allocations
95
+
85
96
let reference = Date ( timeIntervalSinceReferenceDate: 496359355.795410 ) //2016-09-23T14:35:55-0700
86
97
87
- Benchmark ( " allocationsForFixedCalendars " , configuration: . init( scalingFactor: . mega) ) { benchmark in
98
+ let allocationsConfiguration = Benchmark . Configuration (
99
+ metrics: [ . cpuTotal, . mallocCountTotal, . peakMemoryResident, . throughput] ,
100
+ timeUnits: . nanoseconds,
101
+ scalingFactor: . mega
102
+ )
103
+
104
+ Benchmark ( " allocationsForFixedCalendars " , configuration: allocationsConfiguration) { benchmark in
88
105
for _ in benchmark. scaledIterations {
89
106
// Fixed calendar
90
107
let cal = Calendar ( identifier: . gregorian)
@@ -93,7 +110,7 @@ let benchmarks = {
93
110
}
94
111
}
95
112
96
- Benchmark ( " allocationsForCurrentCalendar " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
113
+ Benchmark ( " allocationsForCurrentCalendar " , configuration: allocationsConfiguration ) { benchmark in
97
114
for _ in benchmark. scaledIterations {
98
115
// Current calendar
99
116
let cal = Calendar . current
@@ -102,7 +119,7 @@ let benchmarks = {
102
119
}
103
120
}
104
121
105
- Benchmark ( " allocationsForAutoupdatingCurrentCalendar " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
122
+ Benchmark ( " allocationsForAutoupdatingCurrentCalendar " , configuration: allocationsConfiguration ) { benchmark in
106
123
for _ in benchmark. scaledIterations {
107
124
// Autoupdating current calendar
108
125
let cal = Calendar . autoupdatingCurrent
@@ -111,23 +128,23 @@ let benchmarks = {
111
128
}
112
129
}
113
130
114
- Benchmark ( " copyOnWritePerformance " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
131
+ Benchmark ( " copyOnWritePerformance " , configuration: allocationsConfiguration ) { benchmark in
115
132
var cal = Calendar ( identifier: . gregorian)
116
133
for i in benchmark. scaledIterations {
117
134
cal. firstWeekday = i % 2
118
135
assert ( cal. firstWeekday == i % 2 )
119
136
}
120
137
}
121
138
122
- Benchmark ( " copyOnWritePerformanceNoDiff " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
139
+ Benchmark ( " copyOnWritePerformanceNoDiff " , configuration: allocationsConfiguration ) { benchmark in
123
140
var cal = Calendar ( identifier: . gregorian)
124
141
let tz = TimeZone ( secondsFromGMT: 1800 ) !
125
142
for _ in benchmark. scaledIterations {
126
143
cal. timeZone = tz
127
144
}
128
145
}
129
146
130
- Benchmark ( " allocationsForFixedLocale " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
147
+ Benchmark ( " allocationsForFixedLocale " , configuration: allocationsConfiguration ) { benchmark in
131
148
// Fixed locale
132
149
for _ in benchmark. scaledIterations {
133
150
let loc = Locale ( identifier: " en_US " )
@@ -136,7 +153,7 @@ let benchmarks = {
136
153
}
137
154
}
138
155
139
- Benchmark ( " allocationsForCurrentLocale " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
156
+ Benchmark ( " allocationsForCurrentLocale " , configuration: allocationsConfiguration ) { benchmark in
140
157
// Current locale
141
158
for _ in benchmark. scaledIterations {
142
159
let loc = Locale . current
@@ -145,15 +162,17 @@ let benchmarks = {
145
162
}
146
163
}
147
164
148
- Benchmark ( " allocationsForAutoupdatingCurrentLocale " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
165
+ Benchmark ( " allocationsForAutoupdatingCurrentLocale " , configuration: allocationsConfiguration ) { benchmark in
149
166
// Autoupdating current locale
150
167
for _ in benchmark. scaledIterations {
151
168
let loc = Locale . autoupdatingCurrent
152
169
let identifier = loc. identifier
153
170
assert ( identifier == " en_US " )
154
171
}
155
172
}
156
-
173
+
174
+ // MARK: - Identifiers
175
+
157
176
Benchmark ( " identifierFromComponents " , configuration: . init( scalingFactor: . mega) ) { benchmark in
158
177
let c1 = [ " kCFLocaleLanguageCodeKey " : " en " ]
159
178
let c2 = [ " kCFLocaleLanguageCodeKey " : " zh " ,
0 commit comments