Time two builds with one scratch program, using the build's own compile and link lines
When to use#
You must A/B a code path between two versions of a CMake/Ninja project (for example a git worktree at the old commit and the working tree), no benchmark in the repo covers it, and you must not add one to either tree.
Steps#
Write the timing program in a scratch folder. Use only the public headers. A helper type from the tests (such as a test sink) is not in the library, so define a small one in the program.
In each tree, build a benchmark target that links the same library, so the libraries are current:
cmake --build <tree>/build/build-release --target <bench>.Take that target's compile and link lines from Ninja and swap in your source, object and output:
cc= ln=The last
sedterm points the rpath at the tree's ownlib/, so each binary loads its own libraries.Give each run fresh input (copy the data into a new folder per run), so a cache that one run writes does not serve the next.
Before you move a scratch worktree to another commit, remove the files you copied into it, then copy them back after the checkout. Otherwise
git checkoutrefuses witherror: The following untracked working tree files would be overwritten by checkoutwhen the target commit has the same file.Alternate A and B runs and compare medians. Run nothing else heavy meanwhile.
Evidence#
- dftracer-utils stage 10b (2026-09-27):
qc_bench.cppbuilt against a54be906 and the working tree throughindex_bench's lines. The first try failed witherror: unknown type name 'StringSink'(a type fromtests/trace/views/test_view_common.h); a localNullSink : ExportSinkfixed it. Three alternating runs gave medians within 1.3% between the builds.
Also found by searching
- need to time a code path at old commit vs working tree, no benchmark target exists and I must not add one
- compare performance of a function between two git worktrees of a cmake ninja project
- how do I build a scratch benchmark program against two builds using the project's own compile commands
- compile_commands.json or ninja -t commands to reuse flags for an out-of-tree timing harness linking each library
- how do I time a code path between old commit worktree and current tree when no benchmark exists and I cannot add one
- reuse cmake ninja compile and link command for a scratch benchmark source file
- ninja -t commands to compile my own cpp against a project's libs for A/B comparing two builds