Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Performance of Zephir extension

Hi, I did a simple test to compare the performance of Zephir based extension and original php. But it seems the performance of zephir isn't that good.

Zephir

zephirperformance.zep

namespace ZephirPerformance;

class ZephirPerformance
{
    public static function guid()
    {
        return sprintf("%04X%04X-%04X-%04X-%04X-%04X%04X%04X", 
            mt_rand(0, 65535), mt_rand(0, 65535), 
            mt_rand(0, 65535), mt_rand(16384, 20479), 
            mt_rand(32768, 49151), mt_rand(0, 65535), 
            mt_rand(0, 65535), mt_rand(0, 65535));
    }

    public static function batch(int count = 1)
    {
        int i = 0;
        var guid;
        while i<count{
            let guid = self::guid();
            let i+=1;
        }
    }
}

PerformanceTest.php

use \ZephirPerformance\ZephirPerformance;


class PHPPerformance
{

    public static function guid()
    {
        return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', 
            mt_rand(0, 65535), mt_rand(0, 65535), 
            mt_rand(0, 65535), mt_rand(16384, 20479), 
            mt_rand(32768, 49151), mt_rand(0, 65535), 
            mt_rand(0, 65535), mt_rand(0, 65535));
    }

}

class PerformanceTest extends PHPUnit_Framework_TestCase 
{
    public function test1Speed()
    {
        //warm-up
        ZephirPerformance::guid()."\n";
        PHPPerformance::guid()."\n";
        ZephirPerformance::guid()."\n";
        PHPPerformance::guid()."\n";

        echo "Test 1:\n";
        $start = $this->getMillisecond();
        for ($i=0; $i < 1000000; $i++) { 
            $guid = ZephirPerformance::guid();
        }
        $end = $this->getMillisecond();

        //echo $guid;
        $sp1 = $end - $start;
        echo "Zephire " . $sp1 . " milliseconds\n";

        $start = $this->getMillisecond();
        for ($i=0; $i < 1000000; $i++) { 
            $guid = PHPPerformance::guid();
        }
        $end = $this->getMillisecond();
        $sp2 = $end - $start;
        echo "PHP " . $sp2 . " milliseconds\n";
        echo "".($sp2 - $sp1)/($sp2+0.0)."\n";
    }

    public function test2Speed()
    {
        echo "Test 2:\n";
        //warm-up
        ZephirPerformance::guid()."\n";
        PHPPerformance::guid()."\n";
        ZephirPerformance::guid()."\n";
        PHPPerformance::guid()."\n";

        $start = $this->getMillisecond();
        ZephirPerformance::batch(1000000);
        $end = $this->getMillisecond();

        //echo $guid;
        $sp1 = $end - $start;
        echo "Zephire " . $sp1 . " milliseconds\n";

        $start = $this->getMillisecond();
        for ($i=0; $i < 1000000; $i++) { 
            $guid = PHPPerformance::guid();
        }
        $end = $this->getMillisecond();
        $sp2 = $end - $start;
        echo "PHP " . $sp2 . " milliseconds\n";
        echo "".($sp2 - $sp1)/($sp2+0.0)."\n";

    }




    function getMillisecond()
    {
        list($s1,$s2)=explode(' ',microtime());
        return (float)sprintf('%.0f',(floatval($s1)+floatval($s2))*1000);
    }

}

Result:

Test1
Zephire 4325 milliseconds
PHP 2462 milliseconds

.Test 2:
Zephire 3936 milliseconds
PHP 2395 milliseconds

Full code: https://github.com/shootsoft/zephir-performance

WHY?



1.0k
Accepted
answer

Could you please try again using the latest zephir (0.6.3a)?

edited May '15

Could you please try again using the latest zephir (0.6.3a)?

Awesome!

.Test 1:

Zephire 1656 milliseconds
PHP 2290 milliseconds

.Test 2:

Zephire 1064 milliseconds
PHP 2205 milliseconds