Files
swift-mirror/benchmark/single-source/IntegerToString.swift
Xiaodi Wu 57cf4ce563 [benchmark] Add integer-to-string benchmark (#85209)
A companion to #85180.

<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->
2025-11-03 11:05:26 -05:00

632 lines
13 KiB
Swift

//===--- IntegerToString.swift --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// This test verifies the performance of generating a text description from an
// integer value.
import TestsUtils
public var benchmarks: [BenchmarkInfo] {
var result = [
BenchmarkInfo(
name: "IntegerToString.Int.decimal",
runFunction: run_IntegerToString_Int_decimal,
tags: [.validation, .api, .runtime, .String]
),
BenchmarkInfo(
name: "IntegerToString.Int.hex",
runFunction: run_IntegerToString_Int_hex,
tags: [.validation, .api, .runtime, .String]
),
BenchmarkInfo(
name: "IntegerToString.UInt64.decimal",
runFunction: run_IntegerToString_UInt64_decimal,
tags: [.validation, .api, .runtime, .String]
),
BenchmarkInfo(
name: "IntegerToString.UInt64.hex",
runFunction: run_IntegerToString_UInt64_hex,
tags: [.validation, .api, .runtime, .String]
),
]
if #available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *) {
result.append(
BenchmarkInfo(
name: "IntegerToString.UInt128.decimal",
runFunction: run_IntegerToString_UInt128_decimal,
tags: [.validation, .api, .runtime, .String]
)
)
result.append(
BenchmarkInfo(
name: "IntegerToString.UInt128.hex",
runFunction: run_IntegerToString_UInt128_hex,
tags: [.validation, .api, .runtime, .String]
)
)
}
return result
}
let intValues: [Int] = [
32225,
5550,
-11958,
16730,
19448,
28179,
-5184,
-27885,
27153,
-25416,
-27154,
-4803,
6825,
15717,
5291,
-8487,
-26981,
6916,
-5553,
-28076,
22301,
1491,
-28176,
-16115,
20647,
-26209,
15595,
28922,
-7220,
-21517,
8888,
-20765,
27880,
-31719,
-3556,
-20843,
-8609,
16257,
18387,
-12955,
-8630,
-4484,
9762,
-9688,
16790,
-6094,
-20066,
-10440,
-13443,
28448,
-2807,
-1873,
-914,
14952,
-29547,
-24787,
-16602,
24620,
19172,
-22991,
23742,
-25287,
10448,
-25535,
-29049,
18995,
-16537,
5373,
-6078,
8526,
-21691,
-13917,
-17441,
6693,
29629,
-19710,
-4516,
-32534,
-20677,
18593,
-31752,
16804,
-13985,
31898,
-17588,
-12856,
-24223,
20207,
-5175,
3870,
27002,
1330,
-15751,
-982,
18494,
31689,
171,
-899,
24639,
21605,
172985192,
-813952786,
-1665666121,
185810401,
-356282492,
519153229,
656610213,
1278822001,
-806899184,
-1705231884,
-2046890833,
-564271647,
619274021,
287941721,
-1799461947,
-811488170,
-411507304,
-1335207408,
451872020,
1977308146,
-1223008535,
-288428802,
-1711965056,
-2076021467,
2033055158,
-670842460,
-516315133,
-199699086,
542308014,
-1909344615,
-1966276726,
234662476,
1372605764,
846837306,
527863352,
-828202832,
-748730007,
303844984,
1575846438,
-1464774171,
-457036793,
-591358986,
736683526,
-1371505402,
-1877657819,
357175637,
1345614821,
-924747449,
1989761702,
-1726140877,
675720965,
1157431720,
-311213424,
1741720819,
-598671386,
-848833278,
516543823,
863054577,
-572154114,
-856934927,
537746487,
-94412853,
-1115669214,
-1444942135,
632091775,
-76026126,
1613053059,
-968549322,
-1707832029,
-190729631,
-224041540,
1621150201,
-1711677621,
1673737745,
1418108215,
192137803,
1295494861,
-1501091344,
662914426,
-637338429,
-565927039,
1183026219,
-212021598,
373230108,
-2058247717,
-1767818888,
415563634,
-1418400125,
1937805005,
-526432265,
-1502349122,
497749092,
1490946125,
1059845548,
77216342,
-1909565199,
1936370447,
192817000,
-1327925267,
-1420699487,
]
let uint64Values: [UInt64] = [
40591,
648,
47751,
1130,
26342,
15051,
30182,
12938,
17289,
37368,
60034,
13200,
50423,
61271,
8944,
42897,
2795,
34971,
49282,
56361,
14793,
23164,
27469,
64449,
50461,
64618,
14825,
42722,
34371,
11250,
7160,
63458,
14837,
37036,
53854,
26340,
24874,
34369,
7332,
55162,
6176,
45935,
54363,
23912,
987,
20126,
39147,
49583,
18231,
5667,
30895,
12447,
31660,
30333,
33278,
29789,
21532,
52725,
54855,
32545,
58286,
35152,
53646,
45702,
14329,
54188,
23135,
41244,
22401,
14784,
675,
18229,
41789,
58803,
244,
18288,
28003,
6112,
30579,
11306,
62321,
42339,
8323,
35944,
61043,
16153,
29107,
21502,
6883,
55122,
20687,
60187,
42816,
30866,
30155,
25276,
63954,
38148,
45019,
50680,
15052200827858995124,
15535239661815840417,
2149457961867388524,
12814358425672034158,
11729740424668659213,
17844894807721497052,
11423080310898253432,
10965749284831990256,
10916494852158064740,
12469842768891239891,
1513311992976088619,
13617884549524725528,
533860236860692248,
7712049449353836278,
4960147471138276736,
16926361609825180911,
15068400719203060266,
8417038304930505439,
13245062872331676835,
6424884389759218927,
5961748345383013434,
16815715321918143028,
604315460097471698,
459934484026707022,
9808044054531991973,
6459232136981853232,
17810913071719024129,
11261097629093504173,
4931006069659269673,
2157280141234011162,
1112725691570208272,
9448584584882882851,
14729367995151763490,
16521921619268042500,
14843656943729378712,
17321837091553447968,
4132541032525891440,
4486890304186743693,
4236474307326220888,
1868735020734178432,
9102807780937508528,
12184568678008594796,
9967694486957948860,
17859084903021488220,
9846746388495416233,
8803048912921415034,
10349958310284701040,
4730714293313618055,
18097055857882902700,
17328222097651726003,
13203817601810060088,
2094699121549942609,
8476477969794671415,
17258107258926342578,
14128822154282480330,
4892391481740347145,
13181152331462816702,
3625065524250038171,
1619809144995912262,
13137462691117269747,
9189151219995447797,
4010389930147109246,
8383993102354774029,
10003270414816818325,
5833899563959156305,
18145830297993779151,
10021947356937484992,
14787010438567658715,
11769252621531288267,
3568200047672438073,
17540143499494125889,
12706758128026446125,
6985996994118341105,
14812986274208565444,
4144609439360165223,
15711687169455084514,
14785063337094317578,
10093819834928293597,
17308402634598587341,
9453946139467408203,
8386139843424576389,
17376610657957319884,
1351317592609775051,
7902150678185490998,
16948127234906961413,
14533817965951347865,
6245398895839911653,
5313267148598246580,
15735577773932549567,
2888666416343902415,
3881752705078998798,
13683501860868173977,
12237654837394101036,
7946311886539093727,
17309286086783727919,
13189540567106059382,
2212063115889748914,
4606561406255833845,
13254437290762082565,
17951815598335230834,
]
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
extension UInt128 {
static var _values: [UInt128] {
return [
36486133715007284564051293326217832333,
304267833797578007957957581978974068499,
109881301001772865528086566434944430682,
225888040458180740498287662078515922015,
164534375572811620306521954986782800226,
248312824448870212327604727539082799462,
151659986788138065396574835765204195139,
13723116692520641350788951975726777670,
278099561980080882629829046074700102617,
43611395319615150510489494560201029806,
271671690945284772870687425060116262050,
147021325216068315557209252618900046135,
331474194590916571616373139450048533630,
24049443526263318541449695668028762302,
265976773780110738543033028413475085191,
173091663064191866364155115382519117500,
273925537891612720187993689136218212851,
243505640527110247377122072745298637911,
72213604316012784191255043477783855972,
49422702112416837085419817098851400310,
205472953653809480491088753299587425277,
137803173366712757997429447653727933292,
310894969878565974219526051266804224120,
39310595981499779846991125127623032759,
220948984899922060113131182879908268096,
294266290594410269061369165655535723046,
188827910899191130320995191218825857434,
21251611286023476276984315263897921259,
79974028828191009145421972086630354541,
77801535254714305665903565986591232705,
169824093181153607284747946991580692960,
223517057121755497707143171304065884078,
247115749390089602428776783121803298476,
92097884128464815170556127644177190730,
184857041578877412278557101383872889215,
89091956798806543642434675770382477898,
79929732112360183757064173145268506600,
133144661480026552165531067985206140246,
202280294922209823748109273687295993734,
250659921128321391895121116027712968266,
15099865137588852647793657956057950810,
121051099536529385211916847609587092519,
33729362422406845448526273229964979135,
247664457924359173190964755256262008695,
72484317849372248906068561544960865192,
147198565194719741247922989833107979125,
239110002179008701310121332444762103815,
60749654316560216940575769146040456058,
91291701919780827984693762351441946807,
237088953668773371509123975314546299213,
195602545414002629973025020680204559110,
40036923152119365002904850119018218254,
174265226883707350605146093261612297056,
238411197980524712356302335208001357216,
13607769544676391324611927479512233414,
22397353926508607040358095652489366326,
72663303245598124621031149891911436285,
199572926886549222288653903087354253792,
169989962890466329477644026778867941417,
314067664755982010072336582813647420380,
24386984674354615568649551918272164114,
327081484406343721530400526147937819613,
320806207722331329458451238529869101392,
261532919963917468736956493980018484442,
107045023675125816373002762402636263502,
240185758507934917611105700716539411453,
13708777018090371602215940281314169231,
140802811127263014076476258334406381943,
18564710871282170005145304262666895728,
285905895196831302786638031663391127028,
134727568829868463823380048060689755538,
214770637321725907390984940306204173706,
232559098299332238825902343913290371782,
148880979909460536260588483506781207405,
128870124233958565323058083856837656936,
96786480218539387303576074265723345358,
150696370188037235545582604968523003438,
17597468627522476345950788480213687037,
172632387797748848918818205463370781812,
59946654480271286555097119339910428342,
240845859753182795016428846456886012774,
283280329754803336937423076191391662330,
269149230865210296866902079867339470046,
78596114630871985423328610608681967846,
268278942306959745167911087500423419609,
257743714263065635391901559220149363279,
145421064653226539178721957124838039442,
178529356525517040750328126835452619782,
42274659769744711573895913136965971236,
70372292310600526032671342341226468891,
63028674510203260240907221921907683643,
241881060130886425378022303332112077288,
54625024179246784376179127060398755154,
219391065510237145522446361299097287105,
237941381670743922501810952663739113914,
19434856143627981185033477494786433645,
197125814686346135623800062558015206910,
339686862618755498607335053241557344177,
218424385760691732246344016489643111843,
119126703470689148170122928115934911731,
]
}
}
@inline(never)
public func run_IntegerToString_Int_decimal(n: Int) {
for _ in 0..<(n*10) {
for x in intValues {
blackHole(String(x, radix: 10))
}
}
}
@inline(never)
public func run_IntegerToString_Int_hex(n: Int) {
for _ in 0..<(n*10) {
for x in intValues {
blackHole(String(x, radix: 16))
}
}
}
@inline(never)
public func run_IntegerToString_UInt64_decimal(n: Int) {
for _ in 0..<(n*10) {
for x in uint64Values {
blackHole(String(x, radix: 10))
}
}
}
@inline(never)
public func run_IntegerToString_UInt64_hex(n: Int) {
for _ in 0..<(n*10) {
for x in uint64Values {
blackHole(String(x, radix: 16))
}
}
}
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
@inline(never)
public func run_IntegerToString_UInt128_decimal(n: Int) {
let uint128Values = UInt128._values
for _ in 0..<n {
for x in uint128Values {
blackHole(String(x, radix: 10))
}
}
}
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
@inline(never)
public func run_IntegerToString_UInt128_hex(n: Int) {
let uint128Values = UInt128._values
for _ in 0..<n {
for x in uint128Values {
blackHole(String(x, radix: 16))
}
}
}