Monday, September 30, 2013

My all-time favorite Stack Overflow question/answer

I spend quote a bit of time in Stack Overflow. Not only to answer questions (which I'm not very good at), but also to learn from great answers.

My all-time favorite question/answer is this one, about branch prediction

The question is deceptively simple. There are barely five paragraphs of text and the sample code. 

The first answer, the accepted one, is my all-time favorite answer. 

It is great in at least two-levels:


  • It shows what the CPU is doing behind the scenes. It shows that the CPU is not just slave machine, doing exactly what the lines of code are asking for.

  • It shows what the compilers do behind the scenes, how different optimization levels affect the code, how different compilers behave significantly different from each other. It shows that the compiler is also not a slave translator, creating object code straight out of the code.


For readers with a software-only background, it opens the eyes to the CPU and how much the input to a program affects the performance. 

For readers who skipped (or never took) the compiler class, it opens the eye to the compiler and how it is not a one-to-one translator of source code to object code.

Finally, the comments in the question and in the accepted answer are a great exchange of ideas, including the effects of caching, L1 and L2 caches and many other topics to investigate and learn more.

And the final drop in the bucket of "greatness" is from one of the comments:


One of the few questions ever where the question links to Wikipedia and the Wikipedia entry links to this question. 

The Wikipedia entry is here. The question is in the External Links section and the answer refers to the Wikipedia entry in the text. Talk about circular reference...

And this obfuscated code question is my second favorite question/answer. The question is an exercise on problem solving: how to methodically untangle the question, bit by bit, until it makes sense. It is also incredible that the obfuscated code actually runs.

No comments:

Post a Comment