Why does GCC generate a faster program than Clang in this recursive Fibonacci code?
GCC 4.9.2 in compiler explorer really does loop-unrolling and inlines a lot of function calls while Clang 3.5.1 calls fib
twice each iteration without even tail call optimization like below
fib(int): # @fib(int)
push rbp
push rbx
push rax
mov ebx, edi
cmp ebx, 2
jge .LBB0_1
mov eax, ebx
jmp .LBB0_3
.LBB0_1:
lea edi, dword ptr [rbx - 1]
call fib(int) # fib(ebx - 1)
mov ebp, eax
add ebx, -2
mov edi, ebx
call fib(int) # fib(ebx - 2)
add eax, ebp
.LBB0_3:
add rsp, 8
pop rbx
pop rbp
ret
The GCC version is more than 10 times longer, with only a single fib
call and 20+ labels for inlining the call, which also means that the last call has been tail-optimized into a jmp
or GCC has converted some of the recursion into iteration (since it allocates a big array for storing intermediate values)
I've also brought ICC into perspective, and surprisingly it has 10 call
instructions inside fib
, and it also inlines fib
calls 9 times inside main
, but it doesn't convert the recursive code to iterative
Here's the compiler outputs for comparison
Note that you can modify the code like this to make the output easier to read
int fib(int n) {
if (n<2) return n;
int t = fib(n-1);
return t + fib(n-2);
}
Now compiler explorer will highlight which source code line an instruction in the assembly output corresponds to with distinct colors, and you'll easily see how the two calls are made. The line return t + fib(n-2)
is compiled by GCC to
.L3:
mov eax, DWORD PTR [rsp+112] # n, %sfp
add edx, DWORD PTR [rsp+64] # D.35656, %sfp
add DWORD PTR [rsp+60], edx # %sfp, D.35656
sub DWORD PTR [rsp+104], 2 # %sfp,
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.
However, the way to debug this is to use g++ -S
and clang++ -S
to get the assembly output.
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.
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).