I just tested my convex hull code with clang backend and link time optimization enabled (LTO). Compared to the default used gcc on Linux (4.8.3) the executables are more than 50% smaller and some percent faster!
Enabling LTO for clang can be not really easy, because it needs the gold linker instead of the gnu one. For my Gentoo AMD64 box gcc and the gnu ld linker is the default, and switching to ld.gold as default (with binutils-config) may cause problems for some software, i.e grub2 was mentioned.
Problem is, that clang generally tries to call ld linker -- if this is the gnu tool, LTO fails.
I just found the undocumented -B option for clang 3.5, which allows to specify a custom directory where the linker resides.
So I did:
mkdir /home/stefan/ldgolddir cd ldgolddir ln -s /usr/bin/ld.gold ld
That made LTO working for custom C code as in
clang -flto -O4 -B /home/stefan/ldgolddir/ -o test test.c `pkg-config --libs --cflags gtk+-3.0`
Then I edited nim.cfg
#cc = gcc cc = clang #clang.options.linker = "-ldl" clang.options.linker = "-flto -B /home/stefan/ldgolddir -ldl" #clang.options.speed = "-O3" clang.options.speed = "-O4 -flto -B /home/stefan/ldgolddir"
nim c --threads:on -d:release pconvex_hull.nim ls -l 50392 Oct 31 00:20 pconvex_hull 115942 Oct 30 23:32 pconvex_hull.gcc 1305 Oct 30 03:07 pconvex_hull.nim stefan@AMD64X2 ~/pcon $ ./pconvex_hull @[(x: 0.0, y: 0.0), (x: 999.0, y: 0.0), (x: 999.0, y: 999.0), (x: 0.0, y: 999.0)]
More than 50% size decrease of executable. Similar results for the non parallel program version. And compile time seems to be also shorter. (Generally clang is said to compile faster, use less RAM for compiling and gives better error messages.)
Compile time with clang is in my experience actually considerably shorter. I've rarely found LTO to be worth the effort, though.
Note that there are situations where gcc (at least 4.9) plus will produce faster code; while there are also situations where clang beats gcc (for example, clang can transform a chained if statement into a switch statement, where that's possible), those seem to be rarer.
Still, I find clang much handier for day-to-day development (it helps that it's the default on my Mac, anyway). I use these options in my ~/.config/nim.cfg so that I can switch compilers with -d:gcc49 and -d:gcc48 when needed.
@if gcc48:
cc:gcc
gcc.exe = "gcc-4.8"
@end
@if gcc49:
cc:gcc
gcc.exe = "gcc-4.9"
@end