speed of clang++

Hi, all!

I've just write a test to check clang++ template instantiation speed.

Here it is:

#include <cstring>
#include <iostream>

template <typename parent, size_t N, bool v>
struct entry
{
   typedef parent parent_t;
   typedef entry<entry<parent, N, v>, N - 1, true> a;
   typedef entry<entry<parent, N, v>, N - 1, false> b;

   static const size_t value = a::value + b::value + 1;
};

template <typename parent, bool v>
struct entry<parent, 0, v>
{
   typedef parent parent_t;
   static const size_t value = 1;
};

int main()
{
   std::cout << entry<void, 12, false>::value << std::endl;
   return 0;
}

I've run both clang and g++:

ivan@ivan-desktop:~/d/llvm-build/Release/bin$ time ./clang++ 1.cpp

real 0m1.450s
user 0m1.300s
sys 0m0.130s
ivan@ivan-desktop:~/d/llvm-build/Release/bin$ ./a.out
8191
ivan@ivan-desktop:~/d/llvm-build/Release/bin$ time g++ 1.cpp

real 1m8.859s
user 1m8.520s
sys 0m0.170s
ivan@ivan-desktop:~/d/llvm-build/Release/bin$ ./a.out

8191

Clang is 46 times faster. Why clang is so fast, or why g++ is so slow?

Maybe I have bad g++? Or clang++ has incomplete template support, that is why it is so fast?

g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
clang revision 93461

Clang is 46 times faster. Why clang is so fast, or why g++ is so slow?

G++ keeps template instantiations on a linked list which it has to search linearly, clang uses a hash table.

-Chris

ivan@ivan-desktop:~/d/llvm-build/Release/bin$ time ./clang++ 1.cpp

real 0m1.450s
user 0m1.300s
sys 0m0.130s

ivan@ivan-desktop:~/d/llvm-build/Release/bin$ ./a.out
8191
ivan@ivan-desktop:~/d/llvm-build/Release/bin$ time g++ 1.cpp

real 1m8.859s
user 1m8.520s
sys 0m0.170s

GCC 4.5 is faster, but clang still wins. With -S to test just the compiler:

$ time ~/llvm/llvm-build/Release/bin/clang++ -S
/home/espindola/gcc/test.cc -o t.s

real 0m0.530s
user 0m0.490s
sys 0m0.030s

$ time ~/gcc/inst-x86/bin/g++ -S /home/espindola/gcc/test.cc -o t.s

real 0m1.107s
user 0m1.080s
sys 0m0.030s

Not that 2.09x faster is bad :slight_smile:

Cheers,

Hello cfe-fev,

I don't have all test cases ready, but about a month ago (or two or
so), Chris left me a message to announce (the then unready and
untitled) metatrace, a c++ compile time ray tracer, to cfe-dev.

I guess it is one of the hardest, currently available template
instantiation tests.

If you are interested (even without the test scripts), you can check
out http://gitorious.org/metatrace , the official announcement is at
http://ompf.org/forum/viewtopic.php?p=16900#p16900 .

If there are problems running metatrace through the "c++ interpreter",
I would be glad to help out.

Have a great day,

Sebastian Mach (phresnel)
phresnel@gmail.com