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

  • SEARCH
  • Home
  • 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 5971667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:32:53+00:00 2026-05-22T20:32:53+00:00

I have a XAMPP install, with pretty much the default config. Performance isn’t much

  • 0

I have a XAMPP install, with pretty much the default config.

Performance isn’t much of a problem in general as I use PHP mostly to run web pages and small web apps. Waiting a couple seconds for a page is not unusual.

However, I have recently taken up the problems from Project Euler and decided to do them in PHP.

Try as I may, I couldn’t get my code to run in less than 1 minute 1 second (optimized down from almost 3 min) and I was getting pretty embarrassed, especially considering most posters on Pjt Euler reported times of 1-3 seconds. (#7, find the 10001th prime)

I ported my code to C#, and the same task completed in a blink. 0.4 seconds. Same algorithm, the only notable difference in the code is that I used a List in C# to replace the array I was using in PHP.

While I did expect C# to outperform php, this difference leads me to suspect a gross configuration problem, but I have no idea where to look.

What could be the cause of this poor performance?


Edit: Here is the code:

In PHP:

/*
  * Project Euler #7:
  * By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
  * What is the 10001st prime number?
  */

ini_set('max_execution_time', 300);  
echo "start time:" . date("i:s:u") . "<br />";
function isPrime($number, $prevPrimes)
{       
    foreach ($prevPrimes as $key =>$prime)
    {
        if ($prime == 1)
        {
            continue;
        }
        elseif ($number % $prime == 0)
        {
            return 0;
        }
    }
    // If we get to here, $number is prime
    return $number; 
}
$primes = array();
$i = 0;
$nbPrimes = 0;
while ($nbPrimes <10001)
{
    $i++;
    if ($i % 2 != 0)
    {
        $result = isPrime($i, $primes);

        if ($result != 0)
        {
            $primes[] = $i;
            $nbPrimes++;
        }
    }
}
echo "#$nbPrimes: $result<br>";

echo "End time:" . date("i:s:u") . "<br />";

In C#:

public static void RunSnippet()
{
    Stopwatch stopwatch = new Stopwatch();
    stopwatch.Start();

    List<int> primes = new List<int>();
    int i = 0;
    int nbPrimes = 0;
    int result =0;
    while (nbPrimes <10001)
    {
        i++;
        if (i % 2 != 0)
        {
            result = isPrime(i, primes);

            if (result != 0)
            {
                primes.Add(i);
                nbPrimes++;
            }
        }
    }
    stopwatch.Stop();
    Console.WriteLine("Time elapsed: {0}",
    stopwatch.Elapsed);
    Console.WriteLine ("#" + nbPrimes + ": " + result.ToString());
}
public static int isPrime(int number, List<int> prevPrimes)
{
    foreach (int prime in prevPrimes)
    {
        if (prime == 1)
        {
            continue;
        }
        else if (number % prime == 0)
        {
            return 0;
        }
    }
    // If we get to here, number is prime
    return number;  
}   
  • 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-05-22T20:32:54+00:00Added an answer on May 22, 2026 at 8:32 pm

    FINAL EDIT

    Here is the PHP code from Bakudan’s logic, which returns this result:

    start time:44:25:000000
    #10001: 104759
    End time:44:26:000000
    

    The Code:

    <?php
    echo "start time:" . date("i:s:u") . "\n";
    
    function isPrime($number, &$primes)
    {
        if ($number === 1) return false;
        elseif ($number %2 === 0) return false;
        elseif ($number < 4) return true;
        elseif ($number < 9) return true;
        elseif ($number %3 === 0) return false;
        else $r = floor(sqrt($number));
    
        $f = 5;
        while ($f <= $r) {
            if ($number % $f ===0) return false;
            if ($number % ($f+2) === 0) return false;
            $f = $f + 6;
        }
    
        return true;
    }
    
    $primes = array();
    $nbPrimes = $i = 0;
    while ($nbPrimes < 10001)
    {
        $i++;
        if (isPrime($i, $primes) !== false)
        {
            $primes[] = $i;
            $nbPrimes++;
        }
    }
    echo "#$nbPrimes: " . end($primes) . "\n";
    echo "End time:" . date("i:s:u") . "\n";
    

    Bakudan gave me the pseudo code, I Just translated and wrote it out for the OP’s script above.


    EDIT 2

    I cleaned up the code a bit, didn’t improve anything, may enhance “readability”. But yea, I think this is the best you will get with PHP, which on an i7 without apache yields 5 seconds.

        <?php
        echo "start time:" . date("i:s:u") . "\n";
    
        function isPrime($number, &$primes)
        {
            foreach($primes as $prime) {
                if ($number % $prime === 0 && $prime > 1)
                        return false;
            }
        }
    
        $primes = array();
        $nbPrimes = $i = 1;
        while ($nbPrimes <= 10001)
        {
            if ($i % 2 !== 0 && isPrime($i, $primes) !== false)
            {
                $primes[] = $i;
                $nbPrimes++;
            }
            $i++;
        }
        echo "#$nbPrimes: " . end($primes) . "\n";
        echo "End time:" . date("i:s:u") . "\n";
    

    EDIT

    Knocked another second off by moving the $prime === 1 to be after the $number % $prime check in the same if statement.

    start time:29:40:000000
    #10001: 104743
    End time:29:45:000000
    

    Taking Hannes suggestion of strict checking and passing the array as reference plus adding a few tweaks of my own (modifying the array inside the function):

    ini_set('max_execution_time', 300);
    echo "start time:" . date("i:s:u") . "\n";
    
    function isPrime($number, &$prevPrimes)
    {
       foreach ($prevPrimes as $prime) {
            if ($number % $prime === 0 && $prime !== 1)
            {
                return false;
            }
        }
    
        // If we get to here, $number is prime
        $prevPrimes[] = $number;
        return $number;
    }
    
    $primes = array();
    $i = 0;
    $nbPrimes = 0;
    while ($nbPrimes < 10001)
    {
        $i++;
        if ($i % 2 !== 0)
        {
            $result = isPrime($i, $primes);
    
            if ($result !== 0)
            {
                $nbPrimes++;
            }
        }
    }
    echo "#$nbPrimes: $result\n";
    
    echo "End time:" . date("i:s:u") . "\n";
    

    Which ended up being:

    start time:52:08:000000
    #10001: 104743
    End time:52:15:000000
    

    VS your code:

    start time:50:44:000000
    #10001: 104743
    End time:51:17:000000
    

    A good improvement there, but nothing like C#, just goes to show the power of a compiled language 🙂

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

Sidebar

Related Questions

We have various php projects developed on windows (xampp) that need to be deployed
I'd like to convert a PHP solution to a C++ solution. Currently I have
I'm having an issue where I have a local Wordpress install on my Ubuntu
I have XAMP 1.6.8 and IIS 5.0 installed on my PC(Windows XP SP3). I'm
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you determined a maximum number of characters allowed in FCKEditor ? I seem

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.