drawkcaB | Backward Compatible logo

rants and tips about software

Benchmarking PHP vs C for pure loops and array access

I’m creating a gaming website and one of the games has a complex CPU-intensive AI. Many possible positions for player and computer need to be examined before heuristics can do their work. For this I stack the game state, play a hypothetical move and run AI on it again. The number of moves it looks ahead is configurable, but the more there are the number of possible combinations grows exponentially. This will run on the web server, so it has to consume as little CPU as possible.

The rest of the website is written in PHP, but I started considering something faster for this. Of course, when you want to go fast, you want C or C++. I know some Assembly as well, but it’s PITA to use. But, before “wasting” any time in using C and having to set up the compiler to build the executable for target server, I wanted to make sure there is a reason to do it.

The benchmark is very simple. An array and a big loop doing trivial stuff with it:

------------------- bench.c -------------------
int main(int argc, char** argv)
    int i,j, arr[10] = {0,0,0,0,0,0,0,0,0,0};
    for (i = 0; i < 100; i++)
        for (j = 0; j < 1000000; j++)
            if (arr[i%5+j%5] == 0)
                arr[i%2+j%2] = 1;
    return 0;

I compiled it with GCC 4.2.3. Here’s the PHP version:

------------------- bench.php -----------------
$niz = array(0,0,0,0,0,0,0,0,0,0);
for ($i = 0; $i < 100; $i++)
    for ($j = 0; $j < 1000000; $j++)
        if ($niz[$i%5+$j%5] == 0)
            $niz[$i%3+$j%3] = 1;

I run it with PHP 5.2.5 cli

Now the results:

$ time -p php -f bench.php
real 97.32

$ time -p ./bench
real 2.11

Amazing! C seems to be 46 times faster. I must admit I really expected better results from PHP. I wonder if there is some way to improve PHP speed on this one.

My I guess was that PHP’s excellent duct-tape arrays come with a price. To check this, I removed the array access, leaving only the for loops inside:

$ time -p ./bench
real 0.33

$ time -p php -f bench.php
real 15.14

Looks like array access is not to blame. It is consistently 46 times faster.

Tests were done on Intel Core2Duo CPU clocked at 2.16GHz running 32bit Slackware 12.1. All the results are averages of 10 runs.

Update: it looks like there are other benchmarks, and they come to similar conclusion. Facebook reports PHP being 39 times slower.

Milan Babuškov, 2011-12-01
Copyright © Milan Babu┼íkov 2006-2014