1313import Benchmark
1414import func Benchmark. blackHole
1515
16- #if FOUNDATION_FRAMEWORK
17- import Foundation
18- #else
16+ #if os(macOS) && USE_PACKAGE
1917import FoundationEssentials
2018import FoundationInternationalization
19+ #else
20+ import Foundation
2121#endif
2222
2323let benchmarks = {
2424 Benchmark . defaultConfiguration. maxIterations = 1_000
2525 Benchmark . defaultConfiguration. maxDuration = . seconds( 3 )
2626 Benchmark . defaultConfiguration. scalingFactor = . kilo
27- Benchmark . defaultConfiguration. metrics = [ . cpuTotal, . wallClock , . mallocCountTotal, . throughput]
27+ Benchmark . defaultConfiguration. metrics = [ . cpuTotal, . mallocCountTotal, . throughput]
2828
2929 let thanksgivingComponents = DateComponents ( month: 11 , weekday: 5 , weekdayOrdinal: 4 )
3030 let cal = Calendar ( identifier: . gregorian)
@@ -42,6 +42,7 @@ let benchmarks = {
4242 }
4343 }
4444 }
45+
4546 Benchmark ( " nextThousandThanksgivings " ) { benchmark in
4647 var count = 1000
4748 cal. enumerateDates ( startingAfter: thanksgivingStart, matching: thanksgivingComponents, matchingPolicy: . nextTime) { result, exactMatch, stop in
@@ -51,26 +52,34 @@ let benchmarks = {
5152 }
5253 }
5354 }
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+ }
6066 }
6167 }
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 )
7179 }
72- assert ( count == 1000 )
73- }
80+ } // #available(macOS 15, *)
81+ #endif // swift(>=6.0)
82+
7483 Benchmark ( " CurrentDateComponentsFromThanksgivings " ) { benchmark in
7584 var count = 1000
7685 currentCalendar. enumerateDates ( startingAfter: thanksgivingStart, matching: thanksgivingComponents, matchingPolicy: . nextTime) { result, exactMatch, stop in
@@ -82,9 +91,17 @@ let benchmarks = {
8291 }
8392 }
8493
94+ // MARK: - Allocations
95+
8596 let reference = Date ( timeIntervalSinceReferenceDate: 496359355.795410 ) //2016-09-23T14:35:55-0700
8697
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
88105 for _ in benchmark. scaledIterations {
89106 // Fixed calendar
90107 let cal = Calendar ( identifier: . gregorian)
@@ -93,7 +110,7 @@ let benchmarks = {
93110 }
94111 }
95112
96- Benchmark ( " allocationsForCurrentCalendar " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
113+ Benchmark ( " allocationsForCurrentCalendar " , configuration: allocationsConfiguration ) { benchmark in
97114 for _ in benchmark. scaledIterations {
98115 // Current calendar
99116 let cal = Calendar . current
@@ -102,7 +119,7 @@ let benchmarks = {
102119 }
103120 }
104121
105- Benchmark ( " allocationsForAutoupdatingCurrentCalendar " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
122+ Benchmark ( " allocationsForAutoupdatingCurrentCalendar " , configuration: allocationsConfiguration ) { benchmark in
106123 for _ in benchmark. scaledIterations {
107124 // Autoupdating current calendar
108125 let cal = Calendar . autoupdatingCurrent
@@ -111,23 +128,23 @@ let benchmarks = {
111128 }
112129 }
113130
114- Benchmark ( " copyOnWritePerformance " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
131+ Benchmark ( " copyOnWritePerformance " , configuration: allocationsConfiguration ) { benchmark in
115132 var cal = Calendar ( identifier: . gregorian)
116133 for i in benchmark. scaledIterations {
117134 cal. firstWeekday = i % 2
118135 assert ( cal. firstWeekday == i % 2 )
119136 }
120137 }
121138
122- Benchmark ( " copyOnWritePerformanceNoDiff " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
139+ Benchmark ( " copyOnWritePerformanceNoDiff " , configuration: allocationsConfiguration ) { benchmark in
123140 var cal = Calendar ( identifier: . gregorian)
124141 let tz = TimeZone ( secondsFromGMT: 1800 ) !
125142 for _ in benchmark. scaledIterations {
126143 cal. timeZone = tz
127144 }
128145 }
129146
130- Benchmark ( " allocationsForFixedLocale " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
147+ Benchmark ( " allocationsForFixedLocale " , configuration: allocationsConfiguration ) { benchmark in
131148 // Fixed locale
132149 for _ in benchmark. scaledIterations {
133150 let loc = Locale ( identifier: " en_US " )
@@ -136,7 +153,7 @@ let benchmarks = {
136153 }
137154 }
138155
139- Benchmark ( " allocationsForCurrentLocale " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
156+ Benchmark ( " allocationsForCurrentLocale " , configuration: allocationsConfiguration ) { benchmark in
140157 // Current locale
141158 for _ in benchmark. scaledIterations {
142159 let loc = Locale . current
@@ -145,15 +162,17 @@ let benchmarks = {
145162 }
146163 }
147164
148- Benchmark ( " allocationsForAutoupdatingCurrentLocale " , configuration: . init ( scalingFactor : . mega ) ) { benchmark in
165+ Benchmark ( " allocationsForAutoupdatingCurrentLocale " , configuration: allocationsConfiguration ) { benchmark in
149166 // Autoupdating current locale
150167 for _ in benchmark. scaledIterations {
151168 let loc = Locale . autoupdatingCurrent
152169 let identifier = loc. identifier
153170 assert ( identifier == " en_US " )
154171 }
155172 }
156-
173+
174+ // MARK: - Identifiers
175+
157176 Benchmark ( " identifierFromComponents " , configuration: . init( scalingFactor: . mega) ) { benchmark in
158177 let c1 = [ " kCFLocaleLanguageCodeKey " : " en " ]
159178 let c2 = [ " kCFLocaleLanguageCodeKey " : " zh " ,
0 commit comments