您好,我希望我在正确的社区提出这个问题,如果您认为它不适合这里,请随时将我重定向到更好的地方。
正如我在半年前上大学时了解到的那样,现代 CPU 通常具有用于指令的大型管道,通过能够并行执行许多简单的事情来获得性能。
// A typical code block inside a loop that I fear would clog up a pipeline.
if(l1) a = ...;
elseif(l2) a = ...;
...
else a = ...;
// A different way to write it as a sum that would avoid
// branching but at a possible cost of more instructions (?)
a = (!!l1)*... + (!!l2)*... + ()*...;
现代编译器会知道如何(并允许自己)在第一种情况下避免分支,还是我作为实现者应该努力通过将我的代码规划和重写为逻辑算术表达式来帮助编译器?
我的目标是优化数字运算应用程序的速度。