← All data structures

Binary heap benchmark

C++ (239ms)
C (245ms)
Rust (248ms)
Zig (271ms)
Swift (279ms)
Go (341ms)
C# (346ms)
Java (404ms)
Node.js (704ms)
Bun (855ms)
Python (8,848ms)

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

In-memory array-based binary min-heap (a priority queue) of entries with an i64 key and an i64 value. 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

While writing these solutions the agents could run only the correctness tests (the same workloads at up to 100,000 entries, a few seconds per run). They could not run the timed workloads, time anything, or run any other program, so nothing here was tuned against the benchmark.

Median time per case (ms)

CaseCC++RustGoZigSwiftJavaC#Node.jsBunPython
Push, random2081.02×2131.04×2211.08×2471.20×2151.05×2051.00×3061.49×2961.44×3681.80×1,3696.68×4,76623.25×
Push, descending1621.02×1661.04×1691.06×1711.07×1671.05×1591.00×4883.07×3252.05×8975.65×1,2187.67×15,36696.77×
Build2101.11×2101.11×1891.00×2331.23×4182.21×1901.00×3211.70×2141.13×7854.16×5562.94×6,42734.03×
Pop all2311.08×2141.00×2971.39×4752.22×2431.14×3041.42×4642.17×3321.56×5822.72×5872.75×9,65745.20×
Hold (pop, push)3491.02×3411.00×3791.11×5591.64×3571.05×5101.50×5311.56×5661.66×8622.53×7922.32×11,27533.06×
Hold (replace)4251.16×4091.12×3681.01×5381.47×3651.00×5871.61×5051.38×6411.75×8042.20×7902.16×10,78429.54×
K-way merge3421.06×3491.08×3241.00×5061.56×4191.30×3531.09×5991.85×4081.26×1,3154.06×1,3694.23×13,71042.35×
Top k of a stream2031.10×1851.00×1951.06×2921.58×2171.17×2241.21×2501.35×2451.33×5883.19×9495.14×8,21544.49×
Sort in blocks1551.04×1491.00×1541.03×2281.53×1651.11×1641.10×2691.81×2111.42×4943.32×7334.93×5,95740.04×
Mixed2961.02×2901.00×3111.07×4461.54×3011.04×3971.37×4881.68×4671.61×7462.57×6442.22×7,93427.36×
Overall (geometric mean)2451.02×2391.00×2481.04×3411.42×2711.13×2791.17×4041.69×3461.45×7042.94×8553.57×8,84837.00×

Median time per case, log scale (ms)

Push, random
100 ms1 s10 sSwift: median 205 ms (min 205, max 205; 2 samples)Swift205 msC: median 208 ms (min 208, max 209; 2 samples)C208 msC++: median 213 ms (min 213, max 213; 2 samples)C++213 msZig: median 215 ms (min 215, max 215; 2 samples)Zig215 msRust: median 221 ms (min 220, max 223; 2 samples)Rust221 msGo: median 247 ms (min 247, max 247; 2 samples)Go247 msC#: median 296 ms (min 295, max 296; 2 samples)C#296 msJava: median 306 ms (min 259, max 352; 2 samples)Java306 msNode.js: median 368 ms (min 368, max 369; 2 samples)Node.js368 msBun: median 1,369 ms (min 1,366, max 1,373; 2 samples)Bun1,369 msPython: median 4,766 ms (min 4,752, max 4,779; 2 samples)Python4,766 ms
Push, descending
100 ms1 s10 s100 sSwift: median 159 ms (min 159, max 159; 2 samples)Swift159 msC: median 162 ms (min 162, max 163; 2 samples)C162 msC++: median 166 ms (min 165, max 166; 2 samples)C++166 msZig: median 167 ms (min 166, max 169; 2 samples)Zig167 msRust: median 169 ms (min 168, max 170; 2 samples)Rust169 msGo: median 171 ms (min 168, max 173; 2 samples)Go171 msC#: median 325 ms (min 325, max 325; 2 samples)C#325 msJava: median 488 ms (min 339, max 636; 2 samples)Java488 msNode.js: median 897 ms (min 895, max 899; 2 samples)Node.js897 msBun: median 1,218 ms (min 1,218, max 1,219; 2 samples)Bun1,218 msPython: median 15,366 ms (min 15,363, max 15,370; 2 samples)Python15,366 ms
Build
100 ms1 s10 sRust: median 189 ms (min 189, max 189; 2 samples)Rust189 msSwift: median 190 ms (min 188, max 192; 2 samples)Swift190 msC: median 210 ms (min 210, max 210; 2 samples)C210 msC++: median 210 ms (min 210, max 210; 2 samples)C++210 msC#: median 214 ms (min 214, max 215; 2 samples)C#214 msGo: median 233 ms (min 232, max 233; 2 samples)Go233 msJava: median 321 ms (min 320, max 323; 2 samples)Java321 msZig: median 418 ms (min 412, max 423; 2 samples)Zig418 msBun: median 556 ms (min 555, max 556; 2 samples)Bun556 msNode.js: median 785 ms (min 784, max 785; 2 samples)Node.js785 msPython: median 6,427 ms (min 6,426, max 6,428; 2 samples)Python6,427 ms
Pop all
100 ms1 s10 sC++: median 214 ms (min 211, max 217; 2 samples)C++214 msC: median 231 ms (min 230, max 231; 2 samples)C231 msZig: median 243 ms (min 235, max 251; 2 samples)Zig243 msRust: median 297 ms (min 297, max 298; 2 samples)Rust297 msSwift: median 304 ms (min 301, max 308; 2 samples)Swift304 msC#: median 332 ms (min 326, max 338; 2 samples)C#332 msJava: median 464 ms (min 462, max 466; 2 samples)Java464 msGo: median 475 ms (min 474, max 476; 2 samples)Go475 msNode.js: median 582 ms (min 578, max 585; 2 samples)Node.js582 msBun: median 587 ms (min 580, max 594; 2 samples)Bun587 msPython: median 9,657 ms (min 9,599, max 9,715; 2 samples)Python9,657 ms
Hold (pop, push)
100 ms1 s10 s100 sC++: median 341 ms (min 341, max 341; 2 samples)C++341 msC: median 349 ms (min 343, max 355; 2 samples)C349 msZig: median 357 ms (min 356, max 358; 2 samples)Zig357 msRust: median 379 ms (min 378, max 380; 2 samples)Rust379 msSwift: median 510 ms (min 506, max 514; 2 samples)Swift510 msJava: median 531 ms (min 528, max 534; 2 samples)Java531 msGo: median 559 ms (min 557, max 561; 2 samples)Go559 msC#: median 566 ms (min 566, max 567; 2 samples)C#566 msBun: median 792 ms (min 788, max 796; 2 samples)Bun792 msNode.js: median 862 ms (min 861, max 863; 2 samples)Node.js862 msPython: median 11,275 ms (min 11,270, max 11,280; 2 samples)Python11,275 ms
Hold (replace)
100 ms1 s10 s100 sZig: median 365 ms (min 364, max 366; 2 samples)Zig365 msRust: median 368 ms (min 366, max 370; 2 samples)Rust368 msC++: median 409 ms (min 399, max 419; 2 samples)C++409 msC: median 425 ms (min 424, max 425; 2 samples)C425 msJava: median 505 ms (min 500, max 511; 2 samples)Java505 msGo: median 538 ms (min 537, max 538; 2 samples)Go538 msSwift: median 587 ms (min 573, max 601; 2 samples)Swift587 msC#: median 641 ms (min 640, max 642; 2 samples)C#641 msBun: median 790 ms (min 788, max 792; 2 samples)Bun790 msNode.js: median 804 ms (min 800, max 809; 2 samples)Node.js804 msPython: median 10,784 ms (min 10,764, max 10,803; 2 samples)Python10,784 ms
K-way merge
100 ms1 s10 s100 sRust: median 324 ms (min 323, max 324; 2 samples)Rust324 msC: median 342 ms (min 342, max 343; 2 samples)C342 msC++: median 349 ms (min 349, max 349; 2 samples)C++349 msSwift: median 353 ms (min 352, max 355; 2 samples)Swift353 msC#: median 408 ms (min 407, max 408; 2 samples)C#408 msZig: median 419 ms (min 405, max 433; 2 samples)Zig419 msGo: median 506 ms (min 506, max 507; 2 samples)Go506 msJava: median 599 ms (min 592, max 606; 2 samples)Java599 msNode.js: median 1,315 ms (min 1,312, max 1,318; 2 samples)Node.js1,315 msBun: median 1,369 ms (min 1,366, max 1,372; 2 samples)Bun1,369 msPython: median 13,710 ms (min 13,658, max 13,762; 2 samples)Python13,710 ms
Top k of a stream
100 ms1 s10 sC++: median 185 ms (min 181, max 188; 2 samples)C++185 msRust: median 195 ms (min 194, max 195; 2 samples)Rust195 msC: median 203 ms (min 202, max 204; 2 samples)C203 msZig: median 217 ms (min 216, max 217; 2 samples)Zig217 msSwift: median 224 ms (min 222, max 226; 2 samples)Swift224 msC#: median 245 ms (min 239, max 251; 2 samples)C#245 msJava: median 250 ms (min 247, max 252; 2 samples)Java250 msGo: median 292 ms (min 291, max 292; 2 samples)Go292 msNode.js: median 588 ms (min 575, max 601; 2 samples)Node.js588 msBun: median 949 ms (min 936, max 963; 2 samples)Bun949 msPython: median 8,215 ms (min 8,134, max 8,296; 2 samples)Python8,215 ms
Sort in blocks
100 ms1 s10 sC++: median 149 ms (min 149, max 149; 2 samples)C++149 msRust: median 154 ms (min 154, max 154; 2 samples)Rust154 msC: median 155 ms (min 155, max 155; 2 samples)C155 msSwift: median 164 ms (min 164, max 164; 2 samples)Swift164 msZig: median 165 ms (min 164, max 165; 2 samples)Zig165 msC#: median 211 ms (min 211, max 211; 2 samples)C#211 msGo: median 228 ms (min 227, max 229; 2 samples)Go228 msJava: median 269 ms (min 265, max 272; 2 samples)Java269 msNode.js: median 494 ms (min 469, max 518; 2 samples)Node.js494 msBun: median 733 ms (min 732, max 734; 2 samples)Bun733 msPython: median 5,957 ms (min 5,859, max 6,055; 2 samples)Python5,957 ms
Mixed
100 ms1 s10 sC++: median 290 ms (min 285, max 295; 2 samples)C++290 msC: median 296 ms (min 295, max 297; 2 samples)C296 msZig: median 301 ms (min 300, max 302; 2 samples)Zig301 msRust: median 311 ms (min 309, max 312; 2 samples)Rust311 msSwift: median 397 ms (min 396, max 397; 2 samples)Swift397 msGo: median 446 ms (min 445, max 447; 2 samples)Go446 msC#: median 467 ms (min 455, max 480; 2 samples)C#467 msJava: median 488 ms (min 480, max 496; 2 samples)Java488 msBun: median 644 ms (min 644, max 644; 2 samples)Bun644 msNode.js: median 746 ms (min 745, max 747; 2 samples)Node.js746 msPython: median 7,934 ms (min 7,924, max 7,944; 2 samples)Python7,934 ms

Generation details

LanguageModelEffortGeneratedTime to generateAgent turnsCostLinesTestsSourceToolchain
Cclaude-opus-5-5high2026-11-22 07:30 UTC-07:003m 47s7$0.79289passedheap.c heap.h · logApple clang version 21.0.0 (clang-2100.4.1.1)
C++claude-opus-5-5high2026-11-22 13:25 UTC-07:002m 08s5$0.49201passedheap.hpp · logApple clang version 21.0.0 (clang-2100.4.1.1)
Rustclaude-opus-5-5high2026-11-22 07:29 UTC-07:003m 22s8$0.74287passedheap.rs · logrustc 1.100.0 (0ef1bc386 2026-11-09) (Homebrew)written with rustc 1.99.1 (58cf8f9c3 2026-10-13) (Homebrew)
Goclaude-opus-5-5high2026-11-22 07:28 UTC-07:002m 52s5$0.61240passedheap.go · loggo version go1.27.4 darwin/arm64written with go version go1.27.3 darwin/arm64
Zigclaude-opus-5-5high2026-11-22 07:26 UTC-07:002m 48s11$0.65205passedheap.zig · logzig 0.17.1written with zig 0.17.0
Swiftclaude-opus-5-5high2026-11-22 07:23 UTC-07:001m 58s5$0.46206passedHeap.swift · logApple Swift version 6.4.2 (swiftlang-6.4.2.1.9 clang-2100.4.1.1)
Javaclaude-opus-5-5high2026-11-22 07:24 UTC-07:003m 03s5$0.63247passedHeap.java · logopenjdk version "27.0.1.1" 2026-11-17
C#claude-opus-5-5high2026-11-22 07:22 UTC-07:002m 32s6$0.58266passedHeap.cs · logdotnet 11.0.100
Node.jsclaude-opus-5-5high2026-11-22 07:20 UTC-07:002m 01s6$0.52240passedheap.mjs · lognode v26.13.1
Bunclaude-opus-5-5high2026-11-22 07:19 UTC-07:003m 36s5$0.79254passedheap.mjs · logbun 1.4.5
Pythonclaude-opus-5-5high2026-11-22 07:18 UTC-07:001m 30s5$0.36225passedheap.py · logPython 3.15.2

Peak RSS per case (MB)

CaseCC++RustGoZigSwiftJavaC#Node.jsBunPython
Push, random1,1931,1941,1952401,1941834091,2333396151,345
Push, descending919920919256919181410981338660807
Build151139151235151185422293315348516
Pop all9393105979394327130250259426
Hold (pop, push)197185196304197231562361344694652
Hold (replace)196185196280197231563361297776652
K-way merge9393941899494402221236536475
Top k of a stream2322332464162322186294544609671,698
Sort in blocks474848974848233130143259425
Mixed365255257314365273666536362792988

Test cases

CaseWhat is timed
Push, random8n pushes: 8 times, n entries with random keys are pushed into an empty heap, which grows as it goes.
Push, descending6n pushes: 6 times, n entries with descending keys are pushed into an empty heap, so every new entry travels from a leaf all the way to the root.
Build12 builds of a heap from n entries in random order (12n entries in all).
Pop alln pops that empty a heap of n entries, which returns them in ascending order (the second half of a heap sort).
Hold (pop, push)n steps on a heap of n entries, each a pop of the smallest entry followed by a push of a new, somewhat larger one (the event queue of a simulation).
Hold (replace)The same n steps as the hold case, each done with one replace instead of a pop and a push.
K-way mergeA k-way merge of 4096 sorted runs holding 4n entries in all: 4n steps that each peek at the smallest entry and replace it with the next entry of the same run. The heap holds 4096 entries and stays inside the CPU caches.
Top k of a streamKeeping the n/4 largest of a stream of 4n random entries: n/4 pushes, then a push_pop for each of the remaining entries.
Sort in blocks4n operations: 2 passes that sort the n entries in blocks of 1024 by pushing a block and popping it all, on one heap that is emptied again and again.
Mixed2n operations on a heap of n entries: 50% push, 40% pop, 10% peek. Keys come from a domain of n/16 values, so many entries share a key and are ordered by value.

Method