为什么我的程序这么慢?

gma*_*man 9 c++ benchmarking google-nativeclient

有人决定做一个快速测试,看看本机客户端在速度方面与javascript的比较.他们通过运行10 000 000 sqrt计算并测量所花费的时间来做到这一点.结果用javascript:0.096秒,用NaCl:4.241秒......怎么会这样?速度不是首先使用NaCl的原因之一吗?或者我错过了一些编译器标志或什么?

下面是运行的代码:

clock_t t = clock();
float result = 0;
for(int i = 0; i < 10000000; ++i) {
    result += sqrt(i);
}
t = clock() - t;      
float tt = ((float)t)/CLOCKS_PER_SEC;
pp::Var var_reply = pp::Var(tt);
PostMessage(var_reply);
Run Code Online (Sandbox Code Playgroud)

PS:这个问题是出现在本机客户端邮件列表中的某些内容的编辑版本

gma*_*man 21

注意:此答案是出现在本机客户端邮件列表中的某些内容的编辑版本

微量标记很棘手:除非你非常了解你正在做什么,否则很容易产生与你想要观察/测量的行为无关的苹果到橙子的比较.

我将使用您自己的示例详细说明(我将排除NaCl并坚持使用现有的"经过验证的"技术).

这是您作为本机C程序的测试:

$ cat test1.c
#include <math.h>
#include <time.h>
#include <stdio.h>

int main() {
  clock_t t = clock();
  float result = 0;
  for(int i = 0; i < 1000000000; ++i) {
      result += sqrt(i);
  }
  t = clock() - t;
  float tt = ((float)t)/CLOCKS_PER_SEC;
  printf("%g %g\n", result, tt);

}
$ gcc -std=c99 -O2 test1.c -lm -o test1
$ ./test1
5.49756e+11 25.43
Run Code Online (Sandbox Code Playgroud)

好.我们可以在25.43秒内完成十亿次循环.但是让我们看看需要花费时间:让我们替换"结果+ = sqrt(i);" 用"结果+ = i;"

$ cat test2.c
#include <math.h>
#include <time.h>
#include <stdio.h>

int main() {
  clock_t t = clock();
  float result = 0;
  for(int i = 0; i < 1000000000; ++i) {
      result += i;
  }
  t = clock() - t;
  float tt = ((float)t)/CLOCKS_PER_SEC;
  printf("%g %g\n", result, tt);
}
$ gcc -std=c99 -O2 test2.c -lm -o test2
$ ./test2
1.80144e+16 1.21
Run Code Online (Sandbox Code Playgroud)

哇!95%的时间实际上花费在CPU提供的sqrt函数上,其他一切都花费不到5%.但是如果我们稍微改变一下代码怎么办:替换"printf("%g%g \n",result,tt);" "printf("%g \n",tt);" ?

$ cat test3.c
#include <math.h>
#include <time.h>
#include <stdio.h>

int main() {
  clock_t t = clock();
  float result = 0;
  for(int i = 0; i < 1000000000; ++i) {
      result += sqrt(i);
  }
  t = clock() - t;
  float tt = ((float)t)/CLOCKS_PER_SEC;
  printf("%g\n", tt);
}
$ gcc -std=c99 -O2 test3.c -lm -o test3
$ ./test
1.44
Run Code Online (Sandbox Code Playgroud)

嗯......看起来现在"sqrt"几乎和"+"一样快.怎么会这样?printf如何影响前一个循环AT ALL?

让我们来看看:

$ gcc -std=c99 -O2 test1.c -S -o -
...
.L3:
        cvtsi2sd        %ebp, %xmm1
        sqrtsd  %xmm1, %xmm0
        ucomisd %xmm0, %xmm0
        jp      .L7
        je      .L2
.L7:
        movapd  %xmm1, %xmm0
        movss   %xmm2, (%rsp)
        call    sqrt
        movss   (%rsp), %xmm2
.L2:
        unpcklps        %xmm2, %xmm2
        addl    $1, %ebp
        cmpl    $1000000000, %ebp
        cvtps2pd        %xmm2, %xmm2
        addsd   %xmm0, %xmm2
        unpcklpd        %xmm2, %xmm2
        cvtpd2ps        %xmm2, %xmm2
        jne     .L3
 ...
$ gcc -std=c99 -O2 test3.c -S -o -
...
        xorpd   %xmm1, %xmm1
...
.L5:
        cvtsi2sd        %ebp, %xmm0
        ucomisd %xmm0, %xmm1
        ja      .L14
.L10:
        addl    $1, %ebp
        cmpl    $1000000000, %ebp
        jne     .L5
...
.L14:
        sqrtsd  %xmm0, %xmm2
        ucomisd %xmm2, %xmm2
        jp      .L12
        .p2align 4,,2
        je      .L10
.L12:
        movsd   %xmm1, (%rsp)
        .p2align 4,,5
        call    sqrt
        movsd   (%rsp), %xmm1
        .p2align 4,,4
        jmp     .L10
...
Run Code Online (Sandbox Code Playgroud)

第一个版本实际上调用了sqrt十亿次,但第二个版本根本没有这样做!相反,它检查数字是否为负数,并且仅在这种情况下调用sqrt!为什么?编译器(或编译器作者)在这里尝试做什么?

嗯,这很简单:既然我们没有在这个特定版本中使用"结果",它可以安全地省略"sqrt"调用...如果值不是负数,那就是!如果它是负的那么(取决于FPU标志)sqrt可以做不同的事情(返回无意义的结果,崩溃程序等).这就是为什么这个版本的速度要快十几倍 - 但它根本不计算平方根!

这是最后一个示例,显示了错误的微基准测试的结果:

$ cat test4.c
#include <math.h>
#include <time.h>
#include <stdio.h>

int main() {
  clock_t t = clock();
  int result = 0;
  for(int i = 0; i < 1000000000; ++i) {
      result += 2;
  }
  t = clock() - t;
  float tt = ((float)t)/CLOCKS_PER_SEC;
  printf("%d %g\n", result, tt);
}
$ gcc -std=c99 -O2 test4.c -lm -o test4
$ ./test4
2000000000 0
Run Code Online (Sandbox Code Playgroud)

执行时间是...... ZERO?怎么会这样?亿万计算在少于眨眼之间?让我们来看看:

$ gcc -std=c99 -O2 test1.c -S -o -
...
        call    clock
        movq    %rax, %rbx
        call    clock
        subq    %rbx, %rax
        movl    $2000000000, %edx
        movl    $.LC1, %esi
        cvtsi2ssq       %rax, %xmm0
        movl    $1, %edi
        movl    $1, %eax
        divss   .LC0(%rip), %xmm0
        unpcklps        %xmm0, %xmm0
        cvtps2pd        %xmm0, %xmm0
...
Run Code Online (Sandbox Code Playgroud)

呃,哦,循环完全消除了!所有的计算都发生在编译时,为了加重侮辱,两个"时钟"调用都是在循环体循环之前执行的!

如果我们将它放在单独的功能中怎么办?

$ cat test5.c
#include <math.h>
#include <time.h>
#include <stdio.h>

int testfunc(int num, int max) {
  int result = 0;
  for(int i = 0; i < max; ++i) {
      result += num;
  }
  return result;
}

int main() {
  clock_t t = clock();
  int result = testfunc(2, 1000000000);
  t = clock() - t;
  float tt = ((float)t)/CLOCKS_PER_SEC;
  printf("%d %g\n", result, tt);
}
$ gcc -std=c99 -O2 test5.c -lm -o test5
$ ./test5
2000000000 0
Run Code Online (Sandbox Code Playgroud)

还是一样???怎么会这样?

$ gcc -std=c99 -O2 test5.c -S -o -
...
.globl testfunc
        .type   testfunc, @function
testfunc:
.LFB16:
        .cfi_startproc
        xorl    %eax, %eax
        testl   %esi, %esi
        jle     .L3
        movl    %esi, %eax
        imull   %edi, %eax
.L3:
        rep
        ret
        .cfi_endproc
...
Run Code Online (Sandbox Code Playgroud)

呃哦:编译器很聪明,用乘法代替循环!

现在,如果你在一边添加NaCl而在另一边添加JavaScript,你会得到一个如此复杂的系统,结果实际上是不可预测的.

这里的问题是,对于microbenchmark,你试图隔离一段代码然后评估它的属性,但是编译器(无论是JIT还是AOT)会试图阻止你的努力,因为它试图从你的程序中删除所有无用的计算!

Microbenchmarks很有用,当然,但它们是FORENSIC ANALYSIS工具,而不是你想要用来比较两个不同系统的速度的东西!为此,您需要一些"真实的"(在某种意义上的世界:某些东西无法通过过度热衷的编译器优化成碎片)工作负载:特别是排序算法很受欢迎.

使用sqrt的基准测试尤其令人讨厌,因为正如我们所见,通常他们花费超过90%的时间执行单个CPU指令:sqrtsd(fsqrt,如果它是32位版本),当然,JavaScript和NaCl相同.这些基准(如果正确实施)可以作为试金石(如果某些实现的速度与简单的本机版本的差异太大,那么你做错了什么),但它们作为NaCl,JavaScript,C#的速度比较是无用的或Visual Basic.