/Ot is only one of a number of options that VC takes for optimizing, and it on it's own seems a little pointless. Furthermore, since you didn't provide context about the function you tried to compile there is really no way of knowing why the compiler would do that. It looks to me like there is some room for improvements in it's register allocater, but it could all just be a weird playing together of factors.
Besides, Microsoft hasn't the best reputation for software performance, so judging all compilers by looking just at theirs may be flawed.
Compiling the following (not particularly efficient) implementation of a function to compute greatest common divisor:
Code: Select all
int gcd(int a, int b)
{
if (a == 0)
return b;
else
return gcd(b%a, a);
}
Gcc given the -O2 flag produces the following code:
Code: Select all
.file "test.c"
.text
.p2align 4,,15
.globl gcd
.type gcd, @function
gcd:
.LFB0:
.cfi_startproc
testl %edi, %edi
movl %esi, %eax
jne .L5
jmp .L8
.p2align 4,,10
.p2align 3
.L4:
movl %edi, %eax
movl %edx, %edi
.L5:
movl %eax, %edx
sarl $31, %edx
idivl %edi
testl %edx, %edx
jne .L4
movl %edi, %eax
ret
.L8:
rep
ret
.cfi_endproc
.LFE0:
.size gcd, .-gcd
.ident "GCC: (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2"
.section .note.GNU-stack,"",@progbits
Going over this we immediatly see that it has optimized out all stack access except for the mandatory ret. Giving it a closer look also makes it obvious that it optimized out the tail call into a loop, which is also good for performance, all in all it didn't do that bad, and I for one know that it would take me an awful lot of time to really improve on this.
In almost every practical situation I would choose C/C++ over assembly, you might not be able to reach the same level of efficiency in C as you would in Assembly, but it takes a considerable ammount of skill and time to outperform, and most of us won't have neither the skill nor time required to do it.