|
|
|
|
|
by StillBored
4515 days ago
|
|
I think you forgot to turn on your optimizer. GCC will optimize that loop into something along the lines of line.length+=400000000;
That is because it can predict the loop result during compile time.For example, this is the entire program with your loop and a line to print the result (gcc 4.3.4/linux -O3), mov $0x17d78401,%esi
mov $0x400654,%edi
xor %eax,%eax
jmpq 0x400430 <printf@plt>
In fact without the printf all you get is "reqz ret" (not counting .init overhead in the binary). That is because the compiler detects that line.length is not used and fails to even set it. #include <stdio.h>
int main(int argc,char *argv[])
{
struct lines
{
char *somedata;
int length;
} line;
int i;
line.length=0;
for (i=0;i<=400000000;++i)
{
++line.length;
}
printf("Line len %d\n",line.length);
}
|
|
I did not. I used Pelles C to compile it (with the optimizer turned on). I am not surprised that GCC managed to eliminate the pointless loop entirely in this situation. In fact I was happy that both Pelles C and LuaJIT did not realize that the whole loop was pointless and thus I did not have to come up with a more complex example.
The primary point of this was to show that JIT compilers can optimize away hash table access and dynamic typing in hot loops, not a code generation competition between LuaJIT and GCC.
I am a pretty big LuaJIT fanboy and not even I would claim that Lua compiled with LuaJIT can compete against C compiled by current versions of GCC with all optimizations turned on, at least not in most real-world cases. However, it does get amazingly close, most of the time within an order of magnitude, which means that I personally do not need to use C anymore.