← All data structures

Union-find benchmark

C (202ms)
Rust (206ms)
C++ (213ms)
Swift (216ms)
C# (234ms)
Zig (247ms)
Bun (266ms)
Java (272ms)
Node.js (332ms)
Go (332ms)
Python (8,419ms)

Times are the geometric mean of the language's median times over the 10 tests.

In-memory disjoint-set forest (union-find) over the elements 0 .. n-1, with a union heuristic and path shortening. Every implementation on this page was written by an AI model from the same prompt, then tested and timed by the same harness.

Results: claude-opus-5-5_high

Median time per case (ms)

CaseCC++RustGoZigSwiftJavaC#Node.jsBunPython
Random unions1821.03×2011.14×1761.00×3381.92×2091.19×1891.07×2451.39×2191.25×3662.08×2991.70×5,57931.70×
Chain3601.63×2311.04×2781.25×4361.97×5772.61×3721.68×2751.24×2221.00×2641.19×2261.02×12,81457.81×
Balanced merging69.21.03×75.71.13×67.21.00×1502.23×1131.67×77.51.15×1301.94×1201.79×1632.43×1321.96×9,147136.11×
Find, deep trees2381.00×2541.06×2401.00×3221.35×2521.06×2591.09×3771.58×2551.07×3701.55×2641.11×7,16730.06×
Connected, same set1411.00×1801.27×1851.31×3092.19×1661.18×1651.17×2261.60×1941.38×3292.33×2231.58×16,540117.16×
Connected, random1611.00×2091.30×1941.21×2901.80×1871.16×1641.02×2041.27×1691.05×3061.90×3822.37×8,28951.53×
Set sizes1751.01×1871.07×1741.00×2641.52×1891.09×1881.08×2601.49×2031.17×2911.67×2091.20×8,81450.68×
Grid percolation2181.05×2081.01×2161.04×3041.47×2351.14×2071.00×2581.25×2831.36×3031.46×2131.03×5,87628.38×
Kruskal, small graphs3221.00×3331.04×3231.00×5801.80×3901.21×3351.04×4221.31×3581.11×5251.63×3981.24×9,07128.20×
Mixed, small3831.00×4461.17×3981.04×5561.45×4511.18×4361.14×5171.35×4831.26×6031.58×5011.31×5,92815.50×
Overall (geometric mean)2021.00×2131.05×2061.02×3321.64×2471.22×2161.07×2721.35×2341.15×3321.64×2661.31×8,41941.61×

Median time per case, log scale (ms)

Random unions
100 ms1 s10 sRust: median 176 ms (min 175, max 177; 2 samples)Rust176 msC: median 182 ms (min 181, max 183; 2 samples)C182 msSwift: median 189 ms (min 188, max 189; 2 samples)Swift189 msC++: median 201 ms (min 199, max 204; 2 samples)C++201 msZig: median 209 ms (min 207, max 210; 2 samples)Zig209 msC#: median 219 ms (min 219, max 219; 2 samples)C#219 msJava: median 245 ms (min 243, max 246; 2 samples)Java245 msBun: median 299 ms (min 254, max 344; 2 samples)Bun299 msGo: median 338 ms (min 337, max 338; 2 samples)Go338 msNode.js: median 366 ms (min 307, max 425; 2 samples)Node.js366 msPython: median 5,579 ms (min 5,569, max 5,588; 2 samples)Python5,579 ms
Chain
100 ms1 s10 s100 sC#: median 222 ms (min 220, max 223; 2 samples)C#222 msBun: median 226 ms (min 225, max 226; 2 samples)Bun226 msC++: median 231 ms (min 230, max 232; 2 samples)C++231 msNode.js: median 264 ms (min 263, max 264; 2 samples)Node.js264 msJava: median 275 ms (min 274, max 276; 2 samples)Java275 msRust: median 278 ms (min 276, max 280; 2 samples)Rust278 msC: median 360 ms (min 359, max 362; 2 samples)C360 msSwift: median 372 ms (min 368, max 376; 2 samples)Swift372 msGo: median 436 ms (min 436, max 436; 2 samples)Go436 msZig: median 577 ms (min 572, max 583; 2 samples)Zig577 msPython: median 12,814 ms (min 12,811, max 12,817; 2 samples)Python12,814 ms
Balanced merging
10 ms100 ms1 s10 sRust: median 67.2 ms (min 66.5, max 67.9; 2 samples)Rust67.2 msC: median 69.2 ms (min 69.2, max 69.3; 2 samples)C69.2 msC++: median 75.7 ms (min 75.5, max 75.9; 2 samples)C++75.7 msSwift: median 77.5 ms (min 77.4, max 77.6; 2 samples)Swift77.5 msZig: median 113 ms (min 110, max 116; 2 samples)Zig113 msC#: median 120 ms (min 120, max 121; 2 samples)C#120 msJava: median 130 ms (min 126, max 135; 2 samples)Java130 msBun: median 132 ms (min 131, max 132; 2 samples)Bun132 msGo: median 150 ms (min 146, max 154; 2 samples)Go150 msNode.js: median 163 ms (min 161, max 166; 2 samples)Node.js163 msPython: median 9,147 ms (min 9,131, max 9,163; 2 samples)Python9,147 ms
Find, deep trees
100 ms1 s10 sC: median 238 ms (min 236, max 241; 2 samples)C238 msRust: median 240 ms (min 239, max 240; 2 samples)Rust240 msZig: median 252 ms (min 252, max 253; 2 samples)Zig252 msC++: median 254 ms (min 249, max 258; 2 samples)C++254 msC#: median 255 ms (min 255, max 255; 2 samples)C#255 msSwift: median 259 ms (min 258, max 261; 2 samples)Swift259 msBun: median 264 ms (min 262, max 266; 2 samples)Bun264 msGo: median 322 ms (min 314, max 331; 2 samples)Go322 msNode.js: median 370 ms (min 366, max 375; 2 samples)Node.js370 msJava: median 377 ms (min 376, max 379; 2 samples)Java377 msPython: median 7,167 ms (min 7,145, max 7,188; 2 samples)Python7,167 ms
Connected, same set
100 ms1 s10 s100 sC: median 141 ms (min 141, max 141; 2 samples)C141 msSwift: median 165 ms (min 163, max 167; 2 samples)Swift165 msZig: median 166 ms (min 164, max 168; 2 samples)Zig166 msC++: median 180 ms (min 179, max 181; 2 samples)C++180 msRust: median 185 ms (min 184, max 186; 2 samples)Rust185 msC#: median 194 ms (min 194, max 195; 2 samples)C#194 msBun: median 223 ms (min 219, max 228; 2 samples)Bun223 msJava: median 226 ms (min 213, max 239; 2 samples)Java226 msGo: median 309 ms (min 306, max 311; 2 samples)Go309 msNode.js: median 329 ms (min 328, max 330; 2 samples)Node.js329 msPython: median 16,540 ms (min 16,526, max 16,554; 2 samples)Python16,540 ms
Connected, random
100 ms1 s10 sC: median 161 ms (min 158, max 164; 2 samples)C161 msSwift: median 164 ms (min 162, max 165; 2 samples)Swift164 msC#: median 169 ms (min 158, max 180; 2 samples)C#169 msZig: median 187 ms (min 184, max 191; 2 samples)Zig187 msRust: median 194 ms (min 193, max 196; 2 samples)Rust194 msJava: median 204 ms (min 199, max 208; 2 samples)Java204 msC++: median 209 ms (min 195, max 222; 2 samples)C++209 msGo: median 290 ms (min 283, max 297; 2 samples)Go290 msNode.js: median 306 ms (min 305, max 308; 2 samples)Node.js306 msBun: median 382 ms (min 212, max 551; 2 samples)Bun382 msPython: median 8,289 ms (min 8,275, max 8,303; 2 samples)Python8,289 ms
Set sizes
100 ms1 s10 sRust: median 174 ms (min 174, max 174; 2 samples)Rust174 msC: median 175 ms (min 175, max 175; 2 samples)C175 msC++: median 187 ms (min 185, max 189; 2 samples)C++187 msSwift: median 188 ms (min 187, max 188; 2 samples)Swift188 msZig: median 189 ms (min 189, max 190; 2 samples)Zig189 msC#: median 203 ms (min 202, max 203; 2 samples)C#203 msBun: median 209 ms (min 196, max 222; 2 samples)Bun209 msJava: median 260 ms (min 225, max 295; 2 samples)Java260 msGo: median 264 ms (min 261, max 268; 2 samples)Go264 msNode.js: median 291 ms (min 290, max 291; 2 samples)Node.js291 msPython: median 8,814 ms (min 8,792, max 8,836; 2 samples)Python8,814 ms
Grid percolation
100 ms1 s10 sSwift: median 207 ms (min 204, max 210; 2 samples)Swift207 msC++: median 208 ms (min 208, max 209; 2 samples)C++208 msBun: median 213 ms (min 212, max 213; 2 samples)Bun213 msRust: median 216 ms (min 212, max 221; 2 samples)Rust216 msC: median 218 ms (min 216, max 219; 2 samples)C218 msZig: median 235 ms (min 235, max 235; 2 samples)Zig235 msJava: median 258 ms (min 257, max 260; 2 samples)Java258 msC#: median 283 ms (min 279, max 286; 2 samples)C#283 msNode.js: median 303 ms (min 302, max 304; 2 samples)Node.js303 msGo: median 304 ms (min 303, max 305; 2 samples)Go304 msPython: median 5,876 ms (min 5,874, max 5,879; 2 samples)Python5,876 ms
Kruskal, small graphs
100 ms1 s10 sC: median 322 ms (min 321, max 322; 2 samples)C322 msRust: median 323 ms (min 321, max 324; 2 samples)Rust323 msC++: median 333 ms (min 332, max 334; 2 samples)C++333 msSwift: median 335 ms (min 335, max 336; 2 samples)Swift335 msC#: median 358 ms (min 358, max 358; 2 samples)C#358 msZig: median 390 ms (min 390, max 391; 2 samples)Zig390 msBun: median 398 ms (min 371, max 426; 2 samples)Bun398 msJava: median 422 ms (min 421, max 423; 2 samples)Java422 msNode.js: median 525 ms (min 524, max 525; 2 samples)Node.js525 msGo: median 580 ms (min 578, max 581; 2 samples)Go580 msPython: median 9,071 ms (min 9,070, max 9,073; 2 samples)Python9,071 ms
Mixed, small
100 ms1 s10 sC: median 383 ms (min 380, max 385; 2 samples)C383 msRust: median 398 ms (min 395, max 401; 2 samples)Rust398 msSwift: median 436 ms (min 433, max 440; 2 samples)Swift436 msC++: median 446 ms (min 441, max 451; 2 samples)C++446 msZig: median 451 ms (min 444, max 458; 2 samples)Zig451 msC#: median 483 ms (min 480, max 485; 2 samples)C#483 msBun: median 501 ms (min 459, max 543; 2 samples)Bun501 msJava: median 517 ms (min 514, max 521; 2 samples)Java517 msGo: median 556 ms (min 554, max 557; 2 samples)Go556 msNode.js: median 603 ms (min 602, max 604; 2 samples)Node.js603 msPython: median 5,928 ms (min 5,919, max 5,936; 2 samples)Python5,928 ms

Generation details

LanguageModelEffortGeneratedTime to generateAgent turnsCostLinesTestsSourceToolchain
Cclaude-opus-5-5high2026-11-22 16:35 UTC-07:001m 05s6$0.37159passedunionfind.c unionfind.h · logApple clang version 21.0.0 (clang-2100.4.1.1)
C++claude-opus-5-5high2026-11-22 16:38 UTC-07:000m 58s5$0.33112passedunionfind.hpp · logApple clang version 21.0.0 (clang-2100.4.1.1)
Rustclaude-opus-5-5high2026-11-22 16:34 UTC-07:001m 03s5$0.37160passedunionfind.rs · logrustc 1.100.0 (0ef1bc386 2026-11-09) (Homebrew)
Goclaude-opus-5-5high2026-11-22 16:32 UTC-07:000m 58s4$0.32145passedunionfind.go · loggo version go1.27.4 darwin/arm64
Zigclaude-opus-5-5high2026-11-22 16:32 UTC-07:000m 48s5$0.29107passedunionfind.zig · logzig 0.17.1
Swiftclaude-opus-5-5high2026-11-22 16:27 UTC-07:000m 33s5$0.2795passedUnionFind.swift · logApple Swift version 6.4.2 (swiftlang-6.4.2.1.9 clang-2100.4.1.1)
Javaclaude-opus-5-5high2026-11-22 16:30 UTC-07:000m 38s5$0.2686passedUnionFind.java · logopenjdk version "27.0.1.1" 2026-11-17
C#claude-opus-5-5high2026-11-22 16:25 UTC-07:000m 42s5$0.2981passedUnionFind.cs · logdotnet 11.0.100
Node.jsclaude-opus-5-5high2026-11-22 16:47 UTC-07:000m 36s5$0.26105passedunionfind.mjs · lognode v26.13.1
Bunclaude-opus-5-5high2026-11-22 16:47 UTC-07:000m 29s5$0.2574passedunionfind.mjs · logbun 1.4.5
Pythonclaude-opus-5-5high2026-11-22 16:25 UTC-07:001m 36s5$0.40114passedunionfind.py · logPython 3.15.2

Peak RSS per case (MB)

CaseCC++RustGoZigSwiftJavaC#Node.jsBunPython
Random unions9797971479798281185195205489
Chain67676786676722194131159142
Balanced merging8282821168282250143146136310
Find, deep trees143143143193128143373232255288585
Connected, same set112112113147113113296215195229381
Connected, random112112113147113113296215192216602
Set sizes8282821018282250154164148506
Grid percolation7878781137878255162171192566
Kruskal, small graphs373636903636221108122147269
Mixed, small444444994444230127139155361

Test cases

CaseWhat is timed
Random unions4n unions: 4 times, n random pairs of elements are united, starting from n new singletons, which ends with one giant set and many small ones.
Chain28(n-1) unions: 28 times, union(i, i+1) over all the elements, for ascending i in even rounds and descending i in odd rounds (one of the two orders builds a single long path without a union heuristic).
Balanced merging7(n-1) unions: 7 times, the elements in a random order are merged level by level, sets of 1, 2, 4, ... elements in pairs through arbitrary members, until one set is left.
Find, deep trees7n finds of random elements: 7 times, n finds on a forest built (untimed) as deep as the union heuristic allows, sets of 2^20 scattered elements in trees of 20 levels, whose paths the finds shorten.
Connected, same set14n connected queries (14 passes over n pairs) on a forest of sets of 1024 random elements, where 90% of the pairs lie in the same set.
Connected, random14n connected queries (14 passes over n random pairs) on the same forest of sets of 1024 elements, where the pairs are almost never in the same set.
Set sizes10n set_size queries (10 passes over n random elements) on a forest made by n/2 random unions, which leaves sets of very different sizes.
Grid percolationAbout 6n operations: 6 times, the n/2 cells of a 2236 x 2236 grid are opened in random order, each united with its open neighbours (and a virtual top or bottom element), and after every 2236 openings connected asks whether top and bottom are joined.
Kruskal, small graphs20n unions: Kruskal's algorithm on n/16384 graphs of 4096 vertices and 16384 random edges, each on a new 4096-element structure that fits in the CPU caches, all run 20 times; union says whether an edge joins two components.
Mixed, small10n operations in streams of 131072 on new 65536-element structures that fit in the CPU caches: 40% union, 50% connected, 10% set_size of random elements, so that each stream grows singletons into one giant set.

Method