Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8923353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:05:39+00:00 2026-06-15T07:05:39+00:00

I did up 2 versions of a factorial function in PHP for benchmarking, one

  • 0

I did up 2 versions of a factorial function in PHP for benchmarking, one using normal recursion, the other using tail recursion. The latter should be faster but results show otherwise. Am I missing anything here?

My code is as follows, including the benchmark test:

<?php
benchmark();

function benchmark()
{
    $n = 10;
    $multiplier = 10000;
    $functions = array('factorial_recursive', 'factorial_tailRecursive');

    foreach ($functions as $function) {
        $start = microtime(true);
        echo $function . '<br>';
        echo $function($n) . '<br>';
        echo ($multiplier * (microtime(true) - $start)) . '<br><br>';
    }
}

function factorial_recursive($n)
{
    if ($n == 1) {
        return $n;
    }

    return $n * factorial_recursive($n - 1);
}

function factorial_tailRecursive($n, $result = 1)
{
    if ($n == 1) {
        return $result;
    }

    return factorial_tailRecursive($n - 1, $result * $n);
}

Printed output:

factorial_recursive
3628800
2.4199485778809

factorial_tailRecursive
3628800
2.5296211242676

Any insight appreciated – thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:05:40+00:00Added an answer on June 15, 2026 at 7:05 am

    I took your code and modified it slightly for running on the command line (converted br’s to newlines, tweaked the output format). Also, I changed it so that benchmark is run for 25 iterations instead of just one:

    <?php
    foreach (range(1, 25) as $iteration) {
        benchmark($iteration);
    }
    
    function benchmark($iteration)
    {
        $n = 10;
        $multiplier = 10000;
        $functions = array('factorial_recursive', 'factorial_tailRecursive');
    
        echo "\nIteration {$iteration}:\n";
        foreach ($functions as $function) {
            $start = microtime(true);
            echo $function . "\n";
            echo $function($n) . "\n";
            echo ($multiplier * (microtime(true) - $start)) . "\n\n";
        }
    }
    
    function factorial_recursive($n)
    {
        if ($n == 1) {
            return $n;
        }
    
        return $n * factorial_recursive($n - 1);
    }
    
    function factorial_tailRecursive($n, $result = 1)
    {
        if ($n == 1) {
            return $result;
        }
    
        return factorial_tailRecursive($n - 1, $result * $n);
    }
    

    Here is my php cli version:

    $ php --version
    PHP 5.3.10-1ubuntu3.4 with Suhosin-Patch (cli) (built: Sep 12 2012 19:00:43) 
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
        with Xdebug v2.1.0, Copyright (c) 2002-2010, by Derick Rethans
    

    Here are the results of my 25 iterations:

    $ php ./benchmark.php 
    
    Iteration 1:
    factorial_recursive
    3628800
    0.46014785766602
    
    factorial_tailRecursive
    3628800
    0.33855438232422
    
    
    Iteration 2:
    factorial_recursive
    3628800
    0.26941299438477
    
    factorial_tailRecursive
    3628800
    0.26941299438477
    
    
    Iteration 3:
    factorial_recursive
    3628800
    0.27179718017578
    
    factorial_tailRecursive
    3628800
    0.2598762512207
    
    
    Iteration 4:
    factorial_recursive
    3628800
    0.26941299438477
    
    factorial_tailRecursive
    3628800
    0.2598762512207
    
    
    Iteration 5:
    factorial_recursive
    3628800
    0.28848648071289
    
    factorial_tailRecursive
    3628800
    0.28848648071289
    
    
    Iteration 6:
    factorial_recursive
    3628800
    0.27894973754883
    
    factorial_tailRecursive
    3628800
    0.27894973754883
    
    
    Iteration 7:
    factorial_recursive
    3628800
    0.29087066650391
    
    factorial_tailRecursive
    3628800
    0.2598762512207
    
    
    Iteration 8:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.25033950805664
    
    
    Iteration 9:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.2598762512207
    
    
    Iteration 10:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.24795532226562
    
    
    Iteration 11:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.27894973754883
    
    
    Iteration 12:
    factorial_recursive
    3628800
    0.27894973754883
    
    factorial_tailRecursive
    3628800
    0.27894973754883
    
    
    Iteration 13:
    factorial_recursive
    3628800
    0.2598762512207
    
    factorial_tailRecursive
    3628800
    0.25033950805664
    
    
    Iteration 14:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.27179718017578
    
    
    Iteration 15:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.24795532226562
    
    
    Iteration 16:
    factorial_recursive
    3628800
    0.24080276489258
    
    factorial_tailRecursive
    3628800
    0.25033950805664
    
    
    Iteration 17:
    factorial_recursive
    3628800
    0.27179718017578
    
    factorial_tailRecursive
    3628800
    0.25033950805664
    
    
    Iteration 18:
    factorial_recursive
    3628800
    0.24080276489258
    
    factorial_tailRecursive
    3628800
    0.25033950805664
    
    
    Iteration 19:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.25033950805664
    
    
    Iteration 20:
    factorial_recursive
    3628800
    0.25033950805664
    
    factorial_tailRecursive
    3628800
    0.2598762512207
    
    
    Iteration 21:
    factorial_recursive
    3628800
    0.24795532226562
    
    factorial_tailRecursive
    3628800
    0.23841857910156
    
    
    Iteration 22:
    factorial_recursive
    3628800
    0.30040740966797
    
    factorial_tailRecursive
    3628800
    0.29087066650391
    
    
    Iteration 23:
    factorial_recursive
    3628800
    0.30994415283203
    
    factorial_tailRecursive
    3628800
    0.26226043701172
    
    
    Iteration 24:
    factorial_recursive
    3628800
    0.29087066650391
    
    factorial_tailRecursive
    3628800
    0.32186508178711
    
    
    Iteration 25:
    factorial_recursive
    3628800
    0.27894973754883
    
    factorial_tailRecursive
    3628800
    0.30040740966797
    

    Looking at these results, I’d have to say the difference is negligible, too close to call. They’re effectively identical in runtime, at least in terms of Big-O.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why did Firefox main versions start to grow so fast after 2011 april (FF4)?
Are there any gotchas in JDK 6 which did not exist in earlier versions?
Did jquery ajax $.post() method work in Safari 3.2.3 (525.29) or other? I am
Official Rails site states it supports 1.8.x versions, but did anyone try to run
I'm using the Rails 3 Vestal Versions gem: https://github.com/lailsonbm/vestal_versions I'd like to create some
I just created two versions of a function that can be used to process
I have OS X 10.5 set up with the precompiled versions of PHP 5
I'm generating panoramas using the command line versions of the Hugin toolset. At the
I did try samples, demos from Google codes and other resources with WebView ,
i got plugins using 3 different versions of jQuery on my site 1.7.1 1.5.2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.