Download

Any better more optimised way to handle Booleans?

Been reading some coding horror stories recently, and heard that using too many Booleans is terrible for code, but when I look around the web all I find is tutorials throwing out Booleans in code all the time, is there any better more optimised way to handle certain parts of the code? (Apparently if statements if over used can be taxing)

So to add to all the usual performance concerns, we should be worried about Bools??? :confused: What’s the source of that info dude, as its sounds Facebook-suspect… :stuck_out_tongue: What’s more likely is that using too many Bools / If’s leads to badly designed / poorly executing code (which adds to debugging complexity)… Maybe that’s what you’re getting at. But if your graph is spaghetti hell on Tick, then you’re doing something wrong anyway, and need to find more elegant solutions. BTW: Bools can often be replaced by Gates…

Its because of all this joking around with the code from Yandere Sim online, with it using too many IF statements, honestly being fairly new to UE4 and coding stuff, it has me worried if tons of IF statements/bools are gonna cause issues.

I think that this video may give you a better understanding of the situation. “If” statements aren’t that slow. I’d try to explain it, but Creel here will do it better than I ever could:

https://www.youtube.com/watch?v=bVJ-mWWL7cE

The reality is, most compilers are going to optimize them for you anyways. And it’s not about “speed”, but threading. Short version: I wouldn’t worry about it so much, but there is SOME validity to the concern apparently. Just not in the generalized way that you may think.

Edit: I’d like to point out that he mentions the most important information in passing, not in the comparisons. First, that he is running billions of iterations of the samples. Second, that modern CPUs attempt to guess the path to be taken (he does not go into this, but their success rate is actually quite high) and so, in those instances where the cache does not need flushed, no performance impact is evident. Third, he is not a game designer, but a pioneer in optimization on high levels who generally works for companies like Nvidia to optimize neural networks, so his attention to detail in opitimization is not something that would have as much importance in designing video games. Fourth, that sometimes, the compiler outsmarts the programmer, and the branch you created is actually faster than the branchless version, as the compiler recognizes the branch and de-branches it for you in a faster way than we may often attempt.

The short of it is, if Creel’s branchless code is beaten in optimization by the compiler 50% of the time, with his level of expertise, we needn’t worry about it. That is my humble opinion, at least. Modern compilers are awesome, and as stated above: If you’re EventTick is where your entire game lives, branchless programming won’t save that. Just use more events and think of smarter ways to isolate sections of code to only run when needed.