Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I get a faster binary with XCode's llvm vs. clang++ from MacPorts?

Tags:

c++

xcode

llvm

I have written a benchmark method to test my C++ program (which searches a game tree), and I am noticing that compiling with the "LLVM compiler 2.0" option in XCode 4.0.2 gives me a significantly faster binary than if I compile with the latest version of clang++ from MacPorts.

If I understand correctly I am using a clang front-end and llvm back-end in both cases. Has Apple made improvements to their clang/llvm distribution to produce faster binaries for Mac OS? I can't find much information about the project.

Here are the benchmarks my program produces for various compilers, all using -O3 optimization (higher is better):

(Xcode) "gcc 4.2": 38.7
(Xcode) "llvm gcc 4.2": 51.2
(Xcode) "llvm compiler 2.0": 50.6
g++-mp-4.6: 43.4
clang++: 40.6

Also, how do I compile with the clang/llvm XCode is using from the terminal? I can't find the command.

EDIT: The scores I output are "thousands of games per second" which are calculated over a long enough run of the program. Scores are very consistent over multiple runs, and recent major algorithmic improvements have given me 1% - 5% speed ups, for example. A 25% speed up of 40 to 50 is huge for my program.

UPDATE: I wasn't invoking clang++ from the command line with -flto. Now when I compare clang++ -O3 -flto to /Developer/usr/bin/clang++ -O3 -flto from the command line the results are closer, but the Apple one is still 6.5% faster.

Now how to enable link time optimization for gcc? When I try g++ -flto I get the following error:

cc1plus: error: LTO support has not been enabled in this configuration
like image 335
Imran Avatar asked Nov 04 '22 20:11

Imran


1 Answers

Apple LLVM Compiler should be available under /Developer/usr/bin/clang.

I can't think of any particular reason why MacPorts clang++ would generate slower code... I would check whether you're passing in comparable command-line options. One thing that would make a large difference is if you're producing 32-bit code with one compiler, and 64-bit code with the other.

like image 80
servn Avatar answered Nov 10 '22 19:11

servn