Compilers are fun

Compilers are fun
How did we get here...

For some reason, I find myself frequently talking about compiler optimization with friends, and I inevitably end up talking about an experiment I did 2-3 years ago, and then I have to dig up an old Discord conversation I had with a friend where I wrote about this originally.

So now I'm going to write about it here instead so I can just link to this whenever it pops up again. Kind of translating an unorganized Discord thread to a blog post...


Compilers are fun.

I made a simple dumb program:

Basically, it calculates fibonacci sequences a couple million times, and then prints out how many times it ran the loop.

I had an hypothesis that with enough optimization flags, it will just print out the value of todo immediately (1000000) because it will always be that.

So lets try it:

Compiled without optimiation flags we get this:

It's basically the same code, albeit much less readable because all the variable names have been replaced. The outer loop was also turned into a for loop. We can see that local_20 is n, local_1c is b and so on.

Anyway, then we run it with -O3:

Now it just prints out 1000000 immediately!

Neat!