Recursion, Non-Recursion and Tail Recursion Test using GCC -O2 optimization
I got very positive response for my previous post that published test results of Recursion algorithm efficiency testing. Bluestorm and Alex has responsed to above post asking me to try gcc -O2 optimization level. I tried -O2 optimization and here is the results for factorial of 5 using different factorial algorithm implementations: Here is the results for factorial of 30 with GCC -O2 optimizations. I can't understand why this happens. Because documents I have read says that tail recursion is efficient than normal recursion. This article says that modern compiler will turn tail recursive version into machine code equivalent to the iterative version. But I am confused with results I got. Did I do some thing wrong or is there any secrets behind compiler optimization and recursion. I have to find answers to above results. Sphere: Related ContentRelated posts brought to you by Yet Another Related Posts Plugin.



Chances are good that something is being optimized away. Try adding a global integer and incrementing it in the body of each function: you’ll have to wrap your factorial() function to keep it out of the loop.
Also, check my reply in the previous post.