[英]Cache miss penalty on branching
我想知道用2个乘法代替分支是否更快(由于高速缓存未命中罚款)?
这是我的情况:
float dot = rib1.x*-dir.y + rib1.y*dir.x;
if(dot<0){
dir.x = -dir.x;
dir.y = -dir.y;
}
我正在尝试将其替换为:
float dot = rib1.x*-dir.y + rib1.y*dir.x;
int sgn = (dot < 0.0) - (0.0 < dot ); //returns -1 or 1 (no branching here, tested)
dir.x *= sgn;
dir.y *= sgn;
乘法的成本取决于几个因素,是使用32位还是64位浮点数,以及是否启用SSE。 根据此消息来源,两个float乘法的成本为10个周期: http : //www.agner.org/optimize/instruction_tables.pdf
分支机构的成本还取决于几个因素。 根据经验,不必担心代码中的分支。 分支预测器在CPU上的确切行为将定义性能,但是在这种情况下,您应该期望分支充其量是不可预测的,因此这很可能导致许多分支预测错误。 根据此消息来源,分支错误预测的成本为10到30个周期: http : //valgrind.org/docs/manual/cg-manual.html
任何人都可以在此处提供的最佳建议是进行概要分析和测试。 我猜想在现代的Core i7上, if the range of input varies sufficiently as to cause sufficient branch mispredictions as to outweigh the cost of the additional multiplication
, if the range of input varies sufficiently as to cause sufficient branch mispredictions as to outweigh the cost of the additional multiplication
两个乘法应该比分支更快。
假设未命中率为50%,则分支的成本平均为15个周期(30 * 0.5),浮点mul的成本为10个周期。
编辑 :添加了链接,更新了估计的教学成本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.