Announcement

Collapse
No announcement yet.

GCC 6.2 Is Coming Quite Soon

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #11
    [QUOTE=atomsymbol;n889515]
    Originally posted by mlau View Post

    Try this example:

    Code:
    #include <iostream>
    #include <chrono>
    using namespace std;
    
    static unsigned long long fib(int n) {
    if (n < 2)
    return n;
    return fib(n - 1) + fib(n - 2);
    }
    
    int main() {
    chrono::high_resolution_clock::time_point t0 = chrono::high_resolution_clock::now();
    cout << fib(47) << endl;
    cout << chrono::duration_cast<chrono::duration<double>>(chrono::high_resolution_clock::now() - t0).count() << std::endl;
    return 0;
    }


    Not reproducible on my machine.

    Best of 3 runs of each: The gap is wider with plain -O2.

    time g++ -O3 -march=haswell -mtune=haswell -std=c++11 -o fib-gcc fib.cpp

    real 0m0.235s
    user 0m0.203s
    sys 0m0.020s


    time clang++ -O3 -march=haswell -mtune=haswell -std=c++11 -o fib-clang fib.cpp

    real 0m0.429s
    user 0m0.407s
    sys 0m0.020s

    gcc is able to figure out the tail call. Tail calls such as this one are very uncommon in real-world source code.
    How do you know? Did you audit a very large amount of codebases? It clang optimized it and gcc didn't, you'd probably be
    ridiculing the gcc developers for missing such an obvious and easy optimization, yes?

    I Just don't get the hate for gcc. It's a great all-purpose compiler, that isn't he "hot new thing" like clang and therefore
    doesn't have the attention of "rockstar" developers. Given that, the core develepors, few they are, are doing a tremendous
    job keeping up with and surpassing llvm/clang.

    Comment


    • #12

      Originally posted by atomsymbol
      gcc is able to figure out the tail call. Tail calls such as this one are very uncommon in real-world source code.
      The basic implementation of fibonacci is not tail-recursive. Both gcc and clang seem to optimize tail-recursion :

      Code:
      // recursive
      unsigned fib1(unsigned n) {
          return n < 2 ? n : fib1(n - 1) + fib1(n - 2);
      }
      
      // tail-recursive
      unsigned fib2aux(unsigned m, unsigned a, unsigned b) {
          return m < 2 ? b : fib2aux (m - 1, b, a + b);
      }
      unsigned fib2(unsigned n) {
          return n < 2 ? n : fib2aux(n, 0, 1);
      }
      
      // iterative
      unsigned fib3(unsigned n) {
          if (n < 2) return n;
          unsigned f0 = 0, f1 = 1, f;
          for (unsigned i=2; i<=n; i++) {
              f = f1 + f0;
              f0 = f1;
              f1 = f;
          }
          return f;
      }
      gcc (-O3 5.3)
      fib1 7.96872 s
      fib2 8.5e-08 s
      fib3 8.1e-08 s

      clang (-O3 3.8)
      fib1 18.3519 s
      fib2 4.9e-08 s
      fib3 5.8e-08 s

      Comment


      • #13
        But the 5.x/6.x branches support better LTO (they can do it using less ressources).
        These banches also support newer CPUs and features (Xeon Phi with AVX512, if my memory serves me right)
        And as mentioned, thes branches support more recent standards of the C/C++ language.

        Comment

        Working...
        X