开发者

What's slowing for loops/assignment vs. C?

开发者 https://www.devze.com 2022-12-23 22:30 出处:网络
I have a collection of PHP scripts that are extremely CPU intensive, juggling millions of calculations across hundreds of simultaneous users.

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开发者_StackOverflow中文版 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?


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


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&blog=1) but they are still in development.


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

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号