views:

95

answers:

3

I have a collection of PHP scripts that are extremely CPU intensive, juggling millions of calculations across hundreds of simultaneous users.

I'm trying to find a way to speed up the internals of PHP variable assignment, and looping sequences vs C.

Although PHP is obviously loosely typed, is there any way/extension to specifically assign type (assign, not cast, which seems even more expensive) in a C-style fashion?

Here's what I mean.

This is some dummy code in C:

#include <stdio.h>

int main() {
unsigned long add=0;

for(unsigned long x=0;x<100000000;x++) {
    add = x*59328409238;
}
printf("x is %ld\n",add);

}

Pretty self-explanatory -- it loops 100 million times, multiples each iteration by an arbitrary number of some 59 billion, assigns it to a variable and prints it out.

On my Macbook, compiling it and running it produced:

lees-macbook-pro:Desktop lee$ time ./test2
x is 5932840864471590762

real    0m0.266s
user    0m0.253s
sys  0m0.002s

Pretty darn fast!

A similar script in PHP 5.3 CLI...

<?php
for($i=0;$i<100000000;$i++){
    $a=$i*59328409238;
}
echo $a."\n";
?>

... produced:

lees-macbook-pro:Desktop lee$ time /Applications/XAMPP/xamppfiles/bin/php test3.php
5.93284086447E+18

real    0m22.837s
user    0m22.110s
sys  0m0.078s

Over 22 seconds vs 0.2!

I realize PHP is doing a heck of a lot more behind the scenes than this simple C program - but is there any way to make the PHP internals to behave more 'natively' on primitive types and loops?

+1  A: 

That's the tradeoff that you get with dynamic languages. They are a lot slower than C/C++ and you can't do anything about it. This is not limited to PHP. Ruby or Javascript for example have the same problem.

You can do a lot of optimization when dealing with complex code in PHP but these very simple loops won't get any faster.

There are some projects that try to build a compiler that makes native binaries from PHP code (like Facebooks "HipHop" http://developers.facebook.com/news.php?story=358&amp;blog=1) but they are still in development.

Techpriester
A type inferencing compiler can make them as fast as C, but PHP does not have one yet.
Daniel Newby
Daniel, good call, that's pretty much what I was asking-- is there any way to compile types so there's not the expense of the interpreter trying to figure it out 100 million times. Seems like a great focus for a future PHP version!
Lee
+4  A: 

The short answer is no, it is not possible to speed up PHP code in the manner that you desire. It is almost certainly better to optimize your code so you don't have to do such a heavy numerical operation in the first place.

The long answer is that there are two things you can do if you decide to do things the hard (ill-advised) way.

  • You can write your code as an extension to PHP, in C.
  • You can use something like HipHop
Dietrich Epp
A note on HipHop-- I compiled the above to HipHop which only *marginally* improved the performance... it was still in the 13+ seconds range.
Lee
A: 

Maybe this might interest you: http://developers.facebook.com/news.php?story=358&amp;blog=1

They developed compiler that turns PHP into native code. Should help your case.

This project is hosted on github: http://wiki.github.com/facebook/hiphop-php/

Kamil Szot
Copied from above-- A note on HipHop-- I compiled the above to HipHop which only marginally improved the performance... it was still in the 13+ seconds range
Lee