Intel 二进制文件在 M1 MacBook 上是否比 arm64 二进制文件运行得更快?

时间:2021-07-07 02:48:02

标签: c intel compiler-optimization apple-m1 apple-silicon

我在我的 M1 Macbook 上做了一个小基准测试,得到了一个奇怪的结果。 Intel 二进制文件比 arm64 二进制文件运行得更快。我的实验有什么问题?

$ arch
arm64
$ make
arch -x86_64 cc fib.c -o x86.out
arch -arm64 cc fib.c -o arm64.out
file *.out
arm64.out: Mach-O 64-bit executable arm64
x86.out:   Mach-O 64-bit executable x86_64
time ./x86.out
Fibonacci 45 is 1134903170
        8.32 real         7.54 user         0.01 sys
time ./arm64.out
Fibonacci 45 is 1134903170
       10.21 real         9.77 user         0.01 sys

$ cat fib.c
#include <stdio.h>
int fibonacci(int n);
int main() {
  int n = 45;
  // Print out number of characters in n
  printf("Fibonacci %i is %i\n", n, fibonacci(n));
  return 0;
}
int fibonacci(int n) {
  if (n == 0 || n == 1) {
    return n;
  } else {
    return fibonacci(n-1) + fibonacci(n-2);
  }
}

根据评论,我已启用 -O3,但 Intel bin 的运行速度仍然稍快一些。

$ make
arch -x86_64 cc fib.c -o x86.out -O3
arch -arm64 cc fib.c -o arm64.out -O3
file *.out
arm64.out: Mach-O 64-bit executable arm64
x86.out:   Mach-O 64-bit executable x86_64
time ./x86.out
Fibonacci 45 is 1134903170
        3.46 real         3.33 user         0.00 sys
time ./arm64.out
Fibonacci 45 is 1134903170
        3.57 real         3.45 user         0.00 sys

0 个答案:

没有答案