Recursion, Non-Recursion and Tail Recursion Test using GCC -O2 optimization

1Milinda20th Aug 2008algorithms, c programming, programming

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:

rec_sec

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.

recu_o2_30

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 Content

Related posts brought to you by Yet Another Related Posts Plugin.

1 Comment Comments Feed

  1. Andrew (August 21, 2008, 12:45 am).

    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.

Add a Comment