What Does std::generator Cost, Measured?
The standard generator is the most convenient lazy form and the most expensive one measured; the recursive tree form allocates one coroutine frame per node while a plain stack walk runs 39 to 48 times faster; and a second pass over a consumed generator is undefined behavior, not an empty range.
Written November 10, 2026 · Tested with GCC 14 · 13 min read
- The table: the standard generator costs about 1.3 times the hand-rolled one
- The closest published work states one claim, with no table
- The second pass that segfaults
- Where std::generator exists, and where it does not
What this guide is
Two jobs, eight arms, three reps each, run twice at build time and twice the night before, so every number below is one of four medians from the same probe on the same box. Job A sums 10,000,000 xorshift64 values from the fixed seed 0x123456789abcdef0. Job B sums a full binary tree, depth 16, 131,071 nodes, where node i holds the value i and its children are 2i+1 and 2i+2. Every rep of every arm produces the identical sum: 12002406666735941401 for Job A, 8589737985 for Job B. A timing number can never come from a run that computed something different, because the sums are checked on every rep. Heap allocations are counted too, by counting operator new calls, so the cost story is not only seconds.
The five Job A arms are the same workload written five ways: a plain loop, a views::iota pipeline with transform, a hand-rolled C++20 coroutine generator, std::generator from C++23, and an eager vector that is filled first and summed second. The three Job B arms are the same tree walk written three ways: a recursive generator that re-yields through a manual range-for loop, a recursive generator that yields the child generator with co_yield std::ranges::elements_of, and an explicit stack walk with no coroutine at all. The question is the one people ask before reaching for std::generator: what does the standard type cost against the alternatives, and where does the convenience turn into a trap.
The answer is a correction in two directions. std::generator is the most convenient way to write a lazy sequence and the most expensive lazy form measured: 1.57 to 1.80 times a plain loop across the four runs, and 1.27 to 1.31 times a hand-rolled coroutine doing the same work, while a views::iota pipeline ties the plain loop at zero allocations. But the sharper finding is the trap: a second pass over a consumed generator is undefined behavior, and on GCC 14 with libstdc++ 14 it segfaults, reproduced five times across the banking and build runs. And the recursive tree form allocates one coroutine frame per node, 131,071 frames, while an explicit stack walk runs 39 to 48 times faster than the manual re-yield loop.
The table: the standard generator costs about 1.3 times the hand-rolled one
Wall seconds, medians of three runs. Four runs are printed, two banked the night before and two fresh tonight, because two cells moved between runs and this guide reports all four rather than picking the friendlier one. Every rep returned rc=0, and every Job A sum is 12002406666735941401 on every rep of every arm.
Job A, sum 10,000,000 xorshift64 values, fixed seed:
rawplain loop: banked 0.013317 and 0.014503, fresh 0.014922 and 0.013536. Zero heap allocations.iotaviews::iota plus transform: banked 0.014175 and 0.013146, fresh 0.013714 and 0.013131. Zero heap allocations.handgenhand-rolled C++20 coroutine: banked 0.018915 and 0.018644, fresh 0.017876 and 0.018127. One heap allocation, the coroutine frame.stdgenstd::generator, C++23: banked 0.023996 and 0.023966, fresh 0.023377 and 0.023758. One heap allocation, the coroutine frame.eagerfill a vector, then sum: banked 0.053860 and 0.054648, fresh 0.054290 and 0.054787. One heap allocation, the reserved buffer.
Against the plain loop, std::generator costs 1.80, 1.65, 1.57, and 1.76 times across the four runs; the hand-rolled coroutine costs 1.42, 1.29, 1.20, and 1.34; the iota pipeline costs 1.06, 0.91, 0.92, and 0.97; the eager vector costs 4.04, 3.77, 3.64, and 4.05. The std::generator to hand-rolled ratio is the most stable number in the whole probe: 1.27, 1.29, 1.31, and 1.31. The raw and iota ordering flipped between runs, raw faster on the first banked run and iota faster on the other three, so they are a tie, not an ordering: the guide claims the tie, not a winner. The eager arm is the only one whose gap is large enough to matter on its own: filling a vector first costs about four times the plain loop, because the work is the allocation plus two passes over the data instead of one.
Job B, sum a full binary tree, depth 16, 131,071 nodes, expected sum 8589737985, agreed exactly on every rep:
tree_loopmanual re-yield loop: banked 0.013603 and 0.013828, fresh 0.013263 and 0.013810. 131,071 heap allocations.tree_directco_yield elements_of: banked 0.003438 and 0.003174, fresh 0.003451 and 0.003119. 131,071 heap allocations.tree_iterexplicit stack, no coroutine: banked 0.000282 and 0.000296, fresh 0.000341 and 0.000336. 7 heap allocations.
Yielding the child generator directly is 3.96, 4.36, 3.84, and 4.43 times faster than re-yielding through a manual loop: about four times. The iterative stack walk is 48, 47, 39, and 41 times faster than the manual loop, and 12, 11, 10, and 9 times faster than the direct form. The honest headline is that both generator forms lose badly to a plain stack when the job is only to visit every node. The allocation count is the scaling story, not the resume cost: both recursive generator arms allocate 131,071 heap objects, one coroutine frame per tree node, because every recursive call creates a new generator. The iterative walk allocates 7, including the vector growth reallocations.
One outlier stays in the guide: the eager arm on the second fresh run printed 0.0823271 on its third rep against 0.0541738 and 0.0547871 on the other two, a single-rep spike that moved nothing, because the median is 0.0547871. The hand-rolled arm on the first fresh run printed 0.0196988 on its third rep against 0.0177831 and 0.0178763. The 0.013 to 0.055 second Job A cells sit near the spawn-and-iterate floor for 10 million values, so treat the exact ratios as one-machine figures; the std::generator to hand-rolled gap of 1.27 to 1.31 and the eager gap of 3.64 to 4.05 are the stable claims, because they hold across all four runs.
The closest published work states one claim, with no table
The closest published work is a feature introduction, not a measurement, and the distinction is the gap this probe fills. The Microsoft C++ Team Blog piece on std::generator walks through the same two shapes this probe measures: a custom range against a generator on a flat sequence, and a recursive tree walk with yield_all. It makes one performance claim and no table: with a full tree of depth 16, the direct version runs twice as fast as the loop-based version, from one run with no reps stated. This probe adjudicates that claim with reps: the direct form is about four times faster than the loop form here, 3.84 to 4.43 across four runs, so the claim holds in direction and is bigger. The piece has no flat-sequence table at all, which is the table Job A supplies, and it counts no allocations, which is the frame-per-node finding Job B supplies.
The other published reference gives typical values, not measurements. The wyattau generators page lists a table of typical values: one heap allocation per std::generator creation, a frame of about 100 to 300 bytes, and resume and suspend overhead of about 10 to 50 nanoseconds, comparable to a virtual function call. This probe confirms the allocation claim by measurement: exactly one heap object per generator on Job A, and 131,071 on the recursive tree. The resume cost is not measured directly here, because no arm isolates a single resume; the per-element cost is the measured quantity, and the guide does not convert it into a nanosecond-per-resume figure.
A Developer Community thread from 2017 reports co_yield generators 18 times slower than a for loop, but it measures std::experimental::generator on Visual Studio 2017, a pre-standard type on a different compiler nine years ago, with no reps table. It is not the same type, not the same compiler, and not a same-corpus comparison, so this guide does not adjudicate it and does not carry its number.
The second pass that segfaults
A std::generator is a single-pass range: it can be iterated once. The cppreference page for std::generator::begin states the precondition plainly, that the coroutine must be suspended at its initial suspend point, and its Notes state that it is undefined behavior to call begin more than once on the same generator object. There is no separate sentence for the after-full-consumption case, because it is already covered: a second begin is a second begin, whether the first pass finished or not.
Executed, a second range-for over a fully consumed std::generator segfaults with SIGSEGV, rc=-11, on GCC 14 with libstdc++ 14. It reproduced three times on the banking run and twice on each fresh build run, five confirmations in total across the four probe runs, and the isolated reproduction does the same: the first pass sums 15 over 5 values, then the second begin crashes before printing anything. The guide presents this as undefined behavior manifesting as a crash on this implementation, not as a rule that a second begin always crashes: other standard libraries may return an empty range, throw, or also crash, and the guide claims none of those. The practical rule is to treat a generator like a stream, not a container: if the values are needed twice, store them in a vector first, which is exactly the eager arm, at about four times the cost of the plain loop.
The banking run first assumed the benign behavior, that a second pass yields nothing, and the probe corrected it: the first probe run ended PROBE_FAIL on that wrong expectation plus a typo in the expected tree sum, the expectations were corrected, and the full probe re-ran clean. The trap is in the guide because the machine disproved the guess, not because the guess sounded plausible.
Where std::generator exists, and where it does not
The header is present on GCC 14.2.0 and GCC 16.2.0, and absent on GCC 13.3.0 and Clang 18.1.3, where the compile fails with a missing-header error because both use libstdc++ 13 headers. GCC 16 compiling the generator header is worth noting because the same compiler, in this box's extracted-tree install paired with GNU binutils 2.42, cannot compile the print header: the print probe hit the assembler wall on the base64 pseudo-op, while the generator header compiles cleanly. The wall is the assembler, not the compiler: in the gcc:16 container, whose binutils is 2.44, the same print probe compiles and runs, as the ranges guide on this site records.
The feature-test macro confirms the version: __cpp_lib_generator is 202207 on GCC 14.2.0, with __cplusplus 202302, matching the value in the Notes on the cppreference page. The page states that std::generator models view and input_range, and no stronger range category, which is why the single-pass rule follows from the type itself rather than from a usage note. It also describes how co_yield ranges::elements_of works: when that form is evaluated, the range is converted to a generator and added to the stack that holds the enclosing generator, and when a generator iterator is incremented, the coroutine at the top of the stack is resumed. The page's own example is a recursive binary-tree in-order traversal, the same shape as Job B here, with a generator yielding elements_of the left subtree, then its own value, then elements_of the right subtree. The page does not use the words move-only in its prose, so this guide makes no move-only claim from it; its member-function table lists a constructor and operator= with no copy operations listed.
Checklist
- Before choosing std::generator for a hot loop, check the cost: it runs 1.57 to 1.80 times a plain loop here, and 1.27 to 1.31 times a hand-rolled coroutine doing the same work, while a views::iota pipeline ties the loop at zero allocations.
- Before iterating a generator twice, check the pass count: a second begin is undefined behavior, and on GCC 14 with libstdc++ 14 it segfaults; store the values in a vector first if they are needed twice.
- Before writing a recursive generator over a tree, check the frame count: every recursive call creates a new generator, 131,071 frames for depth 16, and an explicit stack walk runs 39 to 48 times faster than the manual re-yield loop.
- Before quoting a ratio from this guide, check all four runs: the raw and iota ordering flipped between runs, so they are a tie, and the exact ratios are one machine, GCC 14, -O2, warm cache.
- Before reusing these numbers, check the setup: WSL2, 32 CPUs, GCC 14.2.0, -std=c++23 -O2, one thread per arm, 10 million xorshift64 values from a fixed seed, sums verified identical on every rep.
Related guides on this site
- GCC 16.2: std::print, ranges::to, and from_range — What Actually Compiles, and the Macro That Does Not Exist
- How Fast Does std::jthread Actually Stop? Measured Shutdown Latency
- Fix CMake "Generator Does Not Match" and Stale-Cache Errors When Switching Ninja and Make
- How Fast Do std::latch and std::barrier Wake Threads? Measured — 9 to 68 Times Faster Than a Condition Variable
Sources
- Microsoft C++ Team Blog, std::generator: Standard Library Coroutine Support — read in full 2026-11-10: the xorshift custom-range against generator comparison, the recursive tree example, and the one performance claim this probe adjudicates, that the direct tree version runs twice as fast as the loop-based version. No table, no reps, no allocation counts.
- cppreference, std::generator::begin — read 2026-11-10: the precondition that the coroutine is suspended at its initial suspend point, and the Notes statement that calling begin more than once on the same generator object is undefined behavior, behind the second-pass trap.
- cppreference, std::generator — read in full 2026-11-10: defined in the generator header since C++23, models view and input_range, co_yield ranges::elements_of adds the nested generator to the enclosing stack, __cpp_lib_generator is 202207L, and the worked example is a recursive binary-tree in-order traversal. Note the canonical URL has no .html suffix; the .html form serves a missing-page notice.
- wyattau, Generators (std::generator) — read 2026-11-10: the typical-value table, one heap allocation per std::generator creation, a frame of about 100 to 300 bytes, resume and suspend overhead of about 10 to 50 nanoseconds. Typical values, not measurements; the allocation claim is confirmed by this probe.
- Veldica, C++20 Coroutines to C++23 std::generator: Hand-Rolled Machinery, Then the Standard Type — the sibling guide: a migration guide from C++20 coroutines to C++23 std::generator, with no timing table and no allocation measurement. This guide asks what the generator costs, not how to convert to it.
- Veldica, C++20 Coroutine Pitfalls: Three Fixtures, Three Compilers, Same Answer — the sibling guide: coroutine pitfalls in prose, with no measurements. This guide measures the cost; it does not re-teach the pitfalls.
- Veldica, C++20 Coroutines + io_uring: A co_await File Read That Actually Runs — the sibling guide: coroutines for async I/O, a different job. This guide measures synchronous generators; it does not measure async reads.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.