Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to improve poor array performance with HHVM?

Tags:

php

hhvm

I was trying to do some benchmarks of php 5.5 vs HHVM and was getting some pretty good results with hhvm. However the performance of bubble sort on HHVM is pretty bad. I am guessing it has something to do with arrays. In the below example when q=1000 hhvm is almost 5x worse than php 5.5. Since in both case since the test was run so many times I don't believe warm up time should be an issue. In both cases they are being fastcgi mode. In the case where q=1000 php5.5 took about 200ms to serve the page vs almost 1000ms for hhvm. I tried using splfixedclass, but its performance on hhvm was pretty bad too. Is there a special class or some special options that will improve array performance in hhvm?

I put an explanation of what exactly I did here: http://letschat.info/php-5-5-vs-hhvm-vs-node-js-benchmark-part-2/

$starttime = microtime(true);

if($_GET['q']!=""){
 $count = $_GET['q'];
} else {
  $count = 100;

}

function  getRandom(){
        $random = array();
        global $count;

        for($i=0;$i<$count;$i++){
                $random[]=rand(1,100);
        }
        return $random;
}

$array = getRandom();

for($i=0;$i<10;$i++) {
        #$i=0;
        //while ($i<$a) {

        $a = count($array);
        $b=$a-1;

        for($j=0; $j < $a; $j++){
                for ($k=0;$k<$b;$k++) {
                        if ($array[$k+1] < $array[$k]) {
                                $t = $array[$k];
                                $array[$k] = $array[$k+1];
                                $array[$k+1] = $t;
                        }
                }

                //++$i;
        }
        $array[$count/2]=rand(1,100);

}
print_r($array);
echo microtime(true) - $starttime;
like image 235
SamFisher83 Avatar asked Dec 26 '22 15:12

SamFisher83


1 Answers

One problem and one possible problem:

  • Put all your code inside a function. We don't JIT pseudo-mains (code in the main body).
  • Run the code in a web request or if you are doing the command line use -v Eval.Jit=true. We disable JITting for scripts since they usually don't run a long time and the warm-up cost is more than the runtime cost.

.

function main() {
  // Do everything
}
main();
like image 69
Paul Tarjan Avatar answered Jan 06 '23 18:01

Paul Tarjan