为什么在此递归斐波那契代码中,GCC生成比Clang更快的程序? [英] Why does GCC generate a faster program than Clang in this recursive Fibonacci code?

查看:85
本文介绍了为什么在此递归斐波那契代码中,GCC生成比Clang更快的程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我测试过的代码:

#include <iostream>
#include <chrono>
using namespace std;

#define CHRONO_NOW                  chrono::high_resolution_clock::now()
#define CHRONO_DURATION(first,last) chrono::duration_cast<chrono::duration<double>>(last-first).count()

int fib(int n) {
    if (n<2) return n;
    return fib(n-1) + fib(n-2);
}

int main() {
    auto t0 = CHRONO_NOW;
    cout << fib(45) << endl;
    cout << CHRONO_DURATION(t0, CHRONO_NOW) << endl;
    return 0;
}

当然,计算斐波那契数字的方法要快得多,但这是一个很好的压力测试,重点在于递归函数调用。
除了使用chrono来测量时间外,代码没有其他内容。

Of course, there are much faster ways of calculating Fibonacci numbers, but this is a good little stress test that focuses on recursive function calls. There's nothing else to the code, other than the use of chrono for measuring time.

首先,我在OS X的Xcode中运行了几次测试(这就是叮当声),使用 -O3 优化。运行大约需要9秒钟。

First I ran the test a couple of times in Xcode on OS X (so that's clang), using -O3 optimization. It took about 9 seconds to run.

然后,我在Ubuntu上用gcc(g ++)编译了相同的代码(再次使用-O3),而该版本只用了大约跑6.3秒!另外,我在Mac上的VirtualBox内运行Ubuntu ,如果有的话,这只会对性能产生负面影响。

Then, I compiled the same code with gcc (g++) on Ubuntu (using -O3 again), and that version only took about 6.3 seconds to run! Also, I was running Ubuntu inside VirtualBox on my mac, which could only affect the performance negatively, if at all.


  • OS X上的Clang->〜9秒

  • 在Ubuntu 上的gcc VirtualBox ->〜6.3秒。

  • Clang on OS X -> ~9 secs
  • gcc on Ubuntu in VirtualBox -> ~6.3 secs.

我知道这些是完全不同的编译器,因此它们的处理方式有所不同,但是所有这些我见过的以gcc和clang为特色的测试仅显示出很小的差异,在某些情况下,差异是相反的(clang更快)。

I know that these are completely different compilers so they do stuff differently, but all the tests I've seen featuring gcc and clang only showed much less of a difference, and in some cases, the difference was the other way around (clang being faster).

那么有什么逻辑上的解释可以解释为什么在这个特定的例子中,gcc击败了clang?

So is there any logical explanation why gcc beats clang by miles in this particular example?

推荐答案

我不会说gcc击败c声英里。在我看来,性能差异(6.3秒vs 9秒)很小。在我的FreeBSD系统上,clang要求26.12秒,gcc要求10.55秒。

I wouldn't say that gcc beats clang by miles. In my opinion, the performance difference (6.3 seconds vs 9 seconds) is rather small. On my FreeBSD system, clang requires 26.12 seconds and gcc requires 10.55 seconds.

但是,调试此方法的方法是使用 g ++ -S clang ++ -S 来获取程序集输出。

However, the way to debug this is to use g++ -S and clang++ -S to get the assembly output.

我在FreeBSD上对此进行了测试系统。汇编语言文件太长,无法在此处发布,但是gcc似乎在Fibonacci计算函数中执行了多个内联级别(那里有20个 fib()调用! ),而clang只是调用 fib(n-1) fib(n-2)而没有内联级别。

I tested this on my FreeBSD system. The assembly language files are too long to post here, but it appears that gcc performs multiple levels of inlining in the Fibonacci calculation function (there were 20 fib() calls in there!) whereas clang simply calls fib(n-1) and fib(n-2) with no levels of inlining.

顺便说一句,我的gcc版本是4.2.1 20070831补丁了[FreeBSD],而clang版本是3.1(branches / release_31 156863)20120523。这些是即将发布的版本与FreeBSD 9.1-RELEAESE基本系统一起使用。 CPU是AMD Turion II Neo N40L双核处理器(1497.54-MHz)。

By the way, my gcc version was 4.2.1 20070831 patched [FreeBSD] and clang version was 3.1 (branches/release_31 156863) 20120523. These were the versions that come with the FreeBSD 9.1-RELEAESE base system. The CPU is AMD Turion II Neo N40L Dual-Core Processor (1497.54-MHz).

这篇关于为什么在此递归斐波那契代码中,GCC生成比Clang更快的程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆