switch语句比for循环快吗?

3
我正在查看由Lourakis和Argyros开发的稀疏束调整库(sba)的源代码。更准确地说,我正在查看以下函数nrmL2xmy,该函数计算两个向量的平方L2差异。下面的代码是从文件sba_levmar.c中从第146行开始复制的:
/* Compute e=x-y for two n-vectors x and y and return the squared L2 norm of e.
 * e can coincide with either x or y. 
 * Uses loop unrolling and blocking to reduce bookkeeping overhead & pipeline
 * stalls and increase instruction-level parallelism; see http://www.abarnett.demon.co.uk/tutorial.html
 */
static double nrmL2xmy(double *const e, const double *const x, const double *const y, const int n)
{
const int blocksize=8, bpwr=3; /* 8=2^3 */
register int i;
int j1, j2, j3, j4, j5, j6, j7;
int blockn;
register double sum0=0.0, sum1=0.0, sum2=0.0, sum3=0.0;

  /* n may not be divisible by blocksize, 
   * go as near as we can first, then tidy up.
   */
  blockn = (n>>bpwr)<<bpwr; /* (n / blocksize) * blocksize; */

  /* unroll the loop in blocks of `blocksize'; looping downwards gains some more speed */
  for(i=blockn-1; i>0; i-=blocksize){
            e[i ]=x[i ]-y[i ]; sum0+=e[i ]*e[i ];
    j1=i-1; e[j1]=x[j1]-y[j1]; sum1+=e[j1]*e[j1];
    j2=i-2; e[j2]=x[j2]-y[j2]; sum2+=e[j2]*e[j2];
    j3=i-3; e[j3]=x[j3]-y[j3]; sum3+=e[j3]*e[j3];
    j4=i-4; e[j4]=x[j4]-y[j4]; sum0+=e[j4]*e[j4];
    j5=i-5; e[j5]=x[j5]-y[j5]; sum1+=e[j5]*e[j5];
    j6=i-6; e[j6]=x[j6]-y[j6]; sum2+=e[j6]*e[j6];
    j7=i-7; e[j7]=x[j7]-y[j7]; sum3+=e[j7]*e[j7];
  }

  /*
   * There may be some left to do.
   * This could be done as a simple for() loop, 
   * but a switch is faster (and more interesting) 
   */

  i=blockn;
  if(i<n){ 
  /* Jump into the case at the place that will allow
   * us to finish off the appropriate number of items. 
   */
    switch(n - i){ 
      case 7 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
      case 6 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
      case 5 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
      case 4 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
      case 3 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
      case 2 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
      case 1 : e[i]=x[i]-y[i]; sum0+=e[i]*e[i]; ++i;
    }
  }

  return sum0+sum1+sum2+sum3;
}

在代码的中间位置(大致上),作者陈述如下:
 /*
   * There may be some left to do.
   * This could be done as a simple for() loop, 
   * but a switch is faster (and more interesting) 
   */

我不明白为什么 switch 比简单的 for 循环更快。所以我的问题是: 这个说法是否正确? 如果是,为什么

3
我没听懂你的观点。for是循环,而switch最多只能算是一个单一的判断。你怎么能将它们进行比较呢? - Sourav Ghosh
1
你正在比较苹果和橙子。 - Jabberwocky
5
看一下这些语句,你会发现它们并没有以 break 结尾,因此会一直执行到下一个语句。这就是一种简单的循环展开 - Some programmer dude
3
这个陈述是否正确?只有一种方法可以找出答案…… - High Performance Mark
有一个 #pragma 可以展开循环。使用已发布的“方法”展开循环并不能有效地提高速度(因为会执行额外的数学运算)。在编译和链接“静态”时进行速度优化将对执行速度产生更大的影响。 - user3629249
显示剩余3条评论
3个回答

6

在这种情况下,使用开关比循环更快,因为循环会多次检查结束条件,而开关只检查一次。这被称为循环展开,优化编译器可以自动执行。


5
所讨论的switch case在所有情况下都使用了fall-through,因此基本上是展开了的for循环。这可能会稍微快一点,因为没有进行比较操作。
考虑到case数量很小,任何性能差异都可以忽略不计,因此从代码可读性的角度来看,for循环更好。

0

例子:只有在数值相等时才能生成输出。当年龄为18或60岁时。不根据大于或小于的数据进行比较。根据相等性比较数据。

For循环:检查数据的值是否小于或大于(在范围内)。 例如:可以判断输入的年龄是否大于18岁且小于60岁。

Switch Case:检查预先指定的数据值。仅限于相等。

根据您的说法,我会选择for循环。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接