Branchless Programming: Why "If" is Sloowww... and what we can do about it!

Creel · 1.7M views · 2020 · 19:25

Open on YouTube

Software performance & optimizationC, C++, Rust, Go & systems languagesComputer architecture & CPUsexplainer

Why Stack Overflow recommends it

Stack Overflow users link it when someone asks how to avoid if/else branching or write branchless code for performance.

Recommended by 1 people across 1 different months, 2020 to 2025.

What it is

A 19-minute programming video by the Creel channel (What's a Creel?) on branchless programming, a technique for speeding up code by avoiding conditional branches. It looks at both high-level and low-level code and shows how replacing `if` statements with arithmetic or bitwise tricks can help the CPU.

What people said

Recommends Prithvi Patel · 2020-09 · Stack Overflow · on “Avoid else if too much in fixedupdate”

There is one blog that might be useful for you. Link for blog: [LINK] . Also, you can try Branchless Programming if you really want to avoid if-else statements. This will increase fps for sure. Link for Branchless programming: [this video]

Cites James Geddes · 2020-07 · Stack Overflow · on “What is the best way to make branchless counters?”

I am working on a counter in Python3 for devices in the field. It tries to contact the device and returns the total number that are online and offline. def count_online(mac_list): online = 0 offline = 0 for mac in mac_list: is_online = poll_device(mac) # ask the device if it is online, return bool if is_online: online += 1 else: offline += 1 return online, offline I have seen " Branchless method to convert false/true to -1/+1? [LINK] " as well as " Why "If" is Sloowww [this video] ", however I need a count of both the online and offline devices. I also appreciate that in Python, trying to go branchless is arguably a bit pointless as it's not a low level language like C. On the other hand, the Hop

Cites K.Nicholas · 2020-11 · Stack Overflow · on “Is one big for loop faster than than two small ones?”

No. As mentioned, you should determine your end condition before you use in it a loop, meaning that you should have int total = height*width; for( int i=0; i < total; ++i ) { // ... } so that total isn't constantly computed during the loop. That said, everyone does it and I would expect the compiler to optimize your version into this version anyway. The real issue, as I see it, is that in the loop you may have to reverse the computation from the total , meaning that you might have to have int total = height*width; for( int i=0; i < total; ++i ) { int x = total / width; int y = total % height; call(x,y); } This would be slower than for( int i=0; i < width; ++i ) { for( int j=0; i < height; ++

Cites Richard Chambers · 2025-04 · Stack Overflow · on “Creating a toUpper function in C”

A couple of ways to do a toUpper() ` function in C. As a note, if you change the return type to char * you can chain this function with other functions that want a character string. Also an elementary precaution is to check that the pointer provided is not NULL or nullptr . That doesn't mean it's a valid string with a zero terminator on the end but passing a NULL pointer is a fairly common error. Another point is that it might be better if you provided a source string buffer and a destination string buffer to allow the source string buffer to be const . This would involve copying each character from the source to the destination but it would allow const strings to be used with the function.