A.K. Ngo | Average intelligence, uncommon common sense.

Archive for December 2008

Dec/08

5

PHP Faster than Python?

One of my friends (bwilliam) pointed out to me this webpage "Python vs PHP, Python runs slower?" It intrigued me since I already did something similar to this before here. It has been said that Python is faster than PHP, but why in this case, is Python clearly slower than PHP? I was hoping that the user comments on that page would have an answer, but most people just posted a better implementation of finding prime numbers. So I took a little bit of time out of my hectic schedule to find the cause.

Here are the two scripts written by Teifion in PHP and Python:

PHP:

<?php
$primeNumbers = array();
$output = ;

for ($i = 2; $i < 100000; $i++)
{
        $divisible = false;

        foreach ($primeNumbers as $number)
        {
                if ($i % $number == 0)
                {
                        $divisible = true;
                }
        }

        if ($divisible == false)
        {
                $primeNumbers[] = $i;
                $output .= $i;
        }
}

echo $output;
?>

(more…)

· · · · · · ·