switch语句比for循环快吗?

switch语句比for循环快吗?,c,performance,for-loop,optimization,switch-statement,C,Performance,For Loop,Optimization,Switch Statement,我正在看Lourakis&Argyros的稀疏束调整库的源代码。更准确地说,我在看下面的函数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 bl

我正在看Lourakis&Argyros的稀疏束调整库的源代码。更准确地说,我在看下面的函数
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;
}
我不明白为什么
开关
比简单的
for
循环快


所以我的问题是:这句话是真的吗?如果是,为什么

在这种情况下,开关速度更快,因为循环会多次检查结束条件,而开关只检查一次。这就是所谓的,优化编译器主要是靠自己完成的。

所讨论的开关案例在所有案例中都使用了fall-through,因此它基本上是一个展开的
for
循环。这很可能会(稍微)快一些,因为没有执行比较操作


考虑到少数情况,任何性能差异都可以忽略不计,因此从代码可读性的角度来看,循环的
会更好。

示例:仅当值匹配时才能生成输出。18岁或60岁时。基于大于或小于的数据不进行比较。基于相等性比较数据

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

开关大小写:检查预先指定的数据值。只相当于


根据你所说的,我将采用for循环。

我没有理解你的意思
for
是一个循环,
switch
充其量只是一个决策。你如何比较它们?你在比较苹果和橙子。看看这些案例,你会发现它们不会以
break
结尾,因此会失败。这只是一种简单的说法。这句话是真的吗?只有一种方法可以找到…顺便说一句,虽然这可能更为优化(特别是如果经常调用函数),但这可能是过早优化的情况(编译器可能能够展开),也是程序员展示某些聪明的一种方法,也是编程中最为优化和聪明的一种方法,它混淆了代码,使其更难理解和维护。
 /*
   * There may be some left to do.
   * This could be done as a simple for() loop, 
   * but a switch is faster (and more interesting) 
   */