hiphop-php試してみた

facebookが開発したhiphop-php試してみた。

これが何かってのは下記参照。

http://blog.candycane.jp/archives/275

wiki
https://github.com/facebook/hiphop-php/wiki

インスコは結構時間かかります。あと入れないといけないライブラリ大杉。

OSはubuntu10.10で/usr/local/hiphop-phpにインストールした。

サンプルコード

<?php
function is_prime($n) {
    if ($n < 2) {
        return false;
    } else if ($n == 2) {
        return true;
    } else if ($n % 2 == 0) {
        return false;
    }
    $i = 3;
    while ($i * $i <= $n) {
        if ($n % $i == 0) {
            return false;
        }
        $i += 2;
    }
    return true;
}

function main() {
    $ans = 0;
    for ($i=0; $i<=2000000; ++$i) {
        if (is_prime($i)) {
            $ans += $i;
        }
    }
    echo $ans . "\n";
}
$start = microtime(true);
main();
$ttfb = microtime(true) - $start;

echo "----------------------------\n";
echo "exec time $ttfb \n";
echo "end.\n";

phpとhphpiで実行して比較してみた。

php


$ /usr/local/php/bin/php euler10.php
142913828922

                                                      • -

exec time 12.92103099823
end.

PHP遅い・・・

hiphop-php

hphpi


$ /usr/local/hiphop-php/src/hphpi/hphpi -f euler10.php
142913828922

                                                      • -

exec time 40.044190883636
end.

えっ

どういうことなの・・・

ペチパーの希望は消えてしまったのか


[2011-02-23追記]

一度コンパイルしてから実行したらphp-cliで実行したときの3倍くらいの速度になりました。


$ /usr/local/hiphop-php/src/hphp/hphp euler10.php --keep-tempdir=1 --log=3
running hphp...
creating temporary directory /tmp/hphp_Lo6ArD ...
parsing inputs...
parsing inputs took 0'00" (6 ms) wall time
pre-optimizing...
pre-optimizing took 0'00" (0 ms) wall time
inferring types...
inferring types took 0'00" (0 ms) wall time
post-optimizing...
post-optimizing took 0'00" (0 ms) wall time
creating CPP files...
creating CPP files took 0'00" (64 ms) wall time
compiling and linking CPP files...

compiling and linking CPP files took 1'18" (78558 ms) wall time
running executable /tmp/hphp_Lo6ArD/program --file euler10.php...
142913828922

                                                      • -

exec time 4.4274728298187
end.
all files saved in /tmp/hphp_Lo6ArD ...
running hphp took 1'23" (83332 ms) wall time

実行してみる


$ /tmp/hphp_Lo6ArD/program --file euler10.php
142913828922

                                                      • -

exec time 4.4373819828033
end.

おおおおお!!

これでペチパーは救われた。