Compiling the Compiler with the Compiler you just Compiled

July 7th, 2005


This is a neat trick to get your compiler(not your program) run faster. I haven’t tried this but it seems very logical to do so

To understand this tongue twister trick, First we need to understand what a comiler is. A compiler is basically a program that translate your programming language in to binary code that your machine can understand. But, it’d be bland if that’s the only thing compiler does to your program. A smart compiler, gcc for example, optimize you code as well. How does it optimize you code? Take this bad code as an example

String a = "Hello worldddddddddddddddddddddd"
for(i=0;i<strlen (a);i++){printf(a);}

This code is highly inefficient since strlen is called everytime it check for for loop. Luckily, if a compiler notice this and know that inside the for loop length of a won’t change. It can compile your program like as if you wrote it

String a = "Hello worldddddddddddddddddddddd"
int length = strlen(a);
for(i=0;i<length;i ++){printf(a);}

and you will get a faster code. Compiler contains tons of compiling trick like this one to make your program run faster. In every new version, it contains more and more trick. That means the program that’s compiled with newer version of compiler is generally faster than the one compiled with a previous version. Yes, that includes the compiler itself.

So, to get the compiler run faster. Is to compile the compiler you just compile with the compiler you just compile. I know that sounds very confusing. Lemme give you an example,

On you machine, you have gcc3.9. Now you download gcc4.0 from the site and since the only compiler you have is gcc3.9. You compile the new gcc4.0 with gcc3.9. Now after you compile gcc4.0, you computer has a compiler that contains more tricks. However, the binary of gcc4.0 doesn’t contain the new trick in gcc4.0 since you compiled it with gcc 3.9. So, if you compile gcc4.0 source with gcc4.0 binary that you just compiled. The newly compiled(the second one) gcc4.0 binary will contain gcc4.0 trick in itself.

Have fun compiling :)

Entry Filed under: Uncategorized, current work, Programming, OSX Tip

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

July 2005
M T W T F S S
    Aug »
 123
45678910
11121314151617
18192021222324
25262728293031

Most Recent Posts