https://lemire.me/blog/2026/03/18/how-many-branches-can-your-cpu-predict/ Skip to content Daniel Lemire's blog Daniel Lemire is a software performance expert. He ranks among the top 2% of scientists globally (Stanford/Elsevier 2025) and is one of GitHub's top 1000 most followed developers. Menu and widgets * My home page * GitHub profile Your business needs help? Get in touch, I offer private talks, training, consulting and I do sponsored open-source projects. Support my work! I do not accept any advertisement. However, you can you can sponsor my open-source work on GitHub. Daniel Lemire started this blog in 2004. It contains 2,357 posts and 16,192 approved comments. Daniel Lemire's blog ranks among the top 50 most popular blogs on Hacker News, a leading tech news aggregation platform. Join over 12,500 email subscribers: [ ][Go!] Follow me on GitHub: Follow me on X: Follow @lemire (Over 30,000 followers)You can follow this blog on telegram. Search for: [ ] [Search] Recent Posts * How many branches can your CPU predict? * Prefix sums at tens of gigabytes per second with ARM NEON * Text formats are everywhere. Why? * You can use newline characters in URLs * How fast do browsers correct UTF-16 strings? Recent Comments * Vincent Sgherzi on How many branches can your CPU predict? * Daniel Lemire on You can use newline characters in URLs * Daniel Lemire on How many branches can your CPU predict? * Joseph DePina on How many branches can your CPU predict? * yolo4now on You can use newline characters in URLs Pages * A short history of technology * About me * Book recommendations * Cognitive biases * Interviews and talks * My bets * My favorite articles * My favorite quotes * My rules * Newsletter * Predictions * Privacy Policy * Recommended video games * Terms of use * Write good papers Archives Archives [Select Month ] Boring stuff * Log in * Entries feed * Comments feed * WordPress.org [Capture-decran-le-2026-03-18-a-17] How many branches can your CPU predict? Modern processors have the ability to execute many instructions per cycle, on a single core. To be able to execute many instructions per cycle in practice, processors predict branches. I have made the point over the years that modern CPUs have an incredible ability to predict branches. It makes benchmarking difficult because if you test on small datasets, you can get surprising results that might not work on real data. My go-to benchmark is a function like so: while (howmany != 0) { val = generate_random_value() if(val is odd) write to buffer decrement howmany } The processor tries to predict the branch (if clause). Because we use random values, the processor should mispredict one time out of two. However, if we repeat multiple times the benchmark, always using the same random values, the processor learns the branches. How many can processors learn? I test using three recent processors. * The AMD Zen 5 processor can predict perfectly 30,000 branches. * The Apple M4 processor can predict perfectly 10,000 branches. * Intel Emerald Rapids can predict perfectly 5,000 branches. Once more I am disappointed by Intel. AMD is doing wonderfully well on this benchmark. [branch_mispredictions-2][branch_mis] My source code is available. Daniel Lemire, "How many branches can your CPU predict?," in Daniel Lemire's blog, March 18, 2026, https://lemire.me/blog/2026/03/18/ how-many-branches-can-your-cpu-predict/. [BibTeX] Published by [a0c6c3] Daniel Lemire A computer science professor at the University of Quebec (TELUQ). View all posts by Daniel Lemire Posted on March 18, 2026Author Daniel LemireCategories 3 thoughts on "How many branches can your CPU predict?" 1. [a1a17a] Joseph DePina says: March 19, 2026 at 4:14 pm The Rust guy at work said that Rust's borrow checker makes the Rust assembly more predictable which makes it easier for the CPU to do branch prediction and that's why Rust is so much faster than less-predictable languages like C and C++. Maybe you could get even higher numbers if you rewrote your benchmark using Rust. Reply 1. [a0c6c3] Daniel Lemire says: March 19, 2026 at 5:22 pm No. My tests have nothing to do with the programming language. Reply 2. [fd2018] Vincent Sgherzi says: March 19, 2026 at 6:53 pm This is not the case, rust is LLVM backed same as C++. There is no magic here with rust being able to hint to the branch predictor. What your co worker probably conflated is a feature where certain rust code can be optimized away since llvm knows certain facts about the code at certain points. This is most often used to describe how rust often eliminates its bounds checking since llvm can infer the size of things making some checks redundant. If you wanted to hint the branch predictor you could use a keyword like likely or unlikely. Reply Leave a Reply Cancel reply Your email address will not be published. [ ] [ ] [ ] [ ] [ ] [ ] [ ] Comment * [ ] Name * [ ] Email * [ ] Website [ ] [ ] Save my name, email, and website in this browser for the next time I comment. [Post Comment] [ ] [ ] [ ] [ ] [ ] [ ] [ ] D[ ] You can also subscribe by email to this blog (non commercial, no ads, weekly email) If you want to post code, consider formatting it with a tool like tohtml. Post navigation Previous Previous post: Prefix sums at tens of gigabytes per second with ARM NEON Terms of use Proudly powered by WordPress