4 ms·
I'm a little surprised nobody is lamenting the performance hit this kind of technique will incur vs just using an if statement. (reaching into my way back mach
by bane 14y ago
I'm a little surprised nobody is lamenting the performance hit this kind of technique will incur vs just using an if statement.
(reaching into my way back machine, ifs essentially compile down to a few comparison instructions (which are often just subtractions) and a jmp instruction (depending on the platform), it's literally built into the processor! For a simple if statement we might be talking a handful of cycles to eval the if vs an extended call stack pumping and dumping exercise)
- neumann_alfred 14y agoFor inner loops etc., having less branch prediction misses or none at all can actually outweigh having to do slightly more complex calculations. http://stackoverflow.com/a/11227902 http://stackoverflow.com/a/11227902
- bane 14y agoI'm actually curious about the internals of how a modern OOP system works internally once it's boiled down to the CPU level. I'd imagine there's still lots of branch prediction issues in complex OOP systems.
- neumann_alfred 14y agoWell, that you don't use OOP for those inner loops is kinda taken for granted I think. That is, you certainly don't program for code beauty first and foremost -- OOP may not hurt in a particular case, but if it does, code beauty may have to go... example: http://www.particleincell.com/2012/memory-code-optimization/ http://www.particleincell.com/2012/memory-code-optimization/ (which is not about OOP per se, and not about branching, but illustrating that knowing what the CPU actually does (not just what it did decades ago) is really important when talking about performance)