Question

I performed this test using a simple factorial function (borrowed the logic from http://avelino.xxx/2014/03/golang-c-and-python-the-benchmark-time)

Regular PHP Code

function fact($n){ 
    if($n===0)return 1;
    return $n*fact($n-1);
}

function calc(){
    $t = 0;
    for($i=0; $i<100000; $i++){
        for($j=0; $j<8; $j++){
            $t += fact($j);
        }
    }
    return $t; 
}

$result = calc();
echo $result."\n";

PHP Using Zephir

$fact = new Utils\Fact();
$result = $fact->calc();
echo $result."\n";

Zephir Code

namespace Utils;

class Fact{
    public function fact(int n) -> int{
        if(n==0){
            return 1;
        }

        return n*this->fact(n - 1);
    }

    public function calc() -> int{
        int i,j,total;
        let total = 0;
        for i in range(0,99999){
            for j in range (0,7){
                let total = total + this->fact(j);
            }
        }
        return total;
    }
}

I executed these snippets using the time command in the following manner:

Regular PHP

time php -c /etc/php5/apache2/php.ini regular.php

Result

591400000
real 0m0.788s
user 0m0.736s
sys 0m0.026s

PHP Using Zephir Class

time php -c /etc/php5/apache2/php.ini zephyr.php

Result

591400000
real 0m1.529s
user 0m1.494s
sys 0m0.024s

HHVM

time hhvm regular.php

Result

591400000
real 0m0.883s
user 0m0.814s
sys 0m0.045s

Question:

As you can see from the results above, regular PHP code seems to have performed better than the one that uses a compiled Zephyr class as a PHP extension. This is what has me confused.

How can the scripted code end up being faster than the compiled one, especially when both employ the same logic? I think I'm missing something here and would be grateful if someone could help me understand this.

EDIT: Looks like others are facing a similar problem with Zephir: Zephir 2x slower

Was it helpful?

Solution

I don't know about zephyr but HHVM is a JIT so you pay extra startup cost so that future runs are faster. Try increasing your benchmark so it takes 1 minute or 10 minutes if you want to really see HHVM perform.

Also, in some situations we decide to not turn on the JIT in command line mode so force it on with -vEval.Jit=true

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top