Programming has all of these little axioms.
Someone observes some kind of behavior from this language or feature, then they try to communicate it, then they make a sweeping generalization that's mostly correct, but when repeating it, nuance tends to get lost.
Heck, I'm probably doing the same right now.
Some of them are in general, some of them are language-specific. Coming from C++, the main concern is about speed:
What I also want to talk about is why.
Part of this is to just talk about some neat, overlooked things and tease out more truth out of those axioms.
I'm also trying to figure out something else, some soft skill involved here that's more language-agnostic, since this obviously applies beyond C++.
credit: http://www.drbio.cornell.edu/pl47/programming/TICPP-2nd-ed-Vol-two/html/TicV2.html
http://gameprogrammingpatterns.com/data-locality.html
Opposite problem: If I had to guess, people don't think there's a cost, or there's a negligible one
At least, people only looking at software. People that work on hardware see this as a potential optimization gain.
More of an opportunity cost, not that branching is inherently slow. Without branch prediction on the hardware, branching has no cost, but no gain.
Sidenote: Again, I've seen UberShader types of setups also avoid branch prediction. (Also, having one shader avoids state changes on the GPU and changing between active shader types.)
Instead of branching if/else in a boolean, they usually have some kind of math operation with a toggle in the term.
//branching version, will compile to jump instruction
if(bApplyShaderTerm == true)
{
texColor += someRandomShaderTerm;
}//multiplication version -- more cycles, but saves on prediction cost on average
//in case of prediction misses
float applyShaderTerm = 1.0f;
texColor = texColor + someRandomShaderTerm * applyShaderTerm;When reading from a hard drive, what's slower:
Reading a 1 MB file, or reading a 1 GB file?
...Or is this even the right question to start from?
Discussion?
Anyone have anything to share along these lines?