Performance (Genetic Algorithm)

Hi there!

I tried a small genetic algorithm with Zephir. I have the same version in PHP and in a .dll compiled using Zephir. The benchmark is disappointing: PHP ==> 1,325 ms Zephir ==> 1,258 ms

How come the performance gain is so small? Is there an option I should activate somewhere too get near-C performance?

(the same algorithm in Go completed in 259 ms, and 341 ms in Node)

Thank you all! :)

Can you give us your test script?

Yes sure!

Below is my zep file:

// combsort.zep

namespace Algo;

class CombSort
{
    public static function launch(const int! size) -> void
    {
        array arr = [];//range(N,0);
        int z;
        int i;

        for z in range(0, size - 1)
        {
            let arr[z] = size - z;
        }


        var len = count(arr);
        var temp = 0.0;
        int gap = len;
        bool swapped = false;
        int iter = 0;
        while gap > 1 || swapped
        {
            let iter = iter + 1;
            if iter > 1000000
            {
                break;
            }
            let swapped = false;
            let gap = self::_newGap(gap);
            let i = 0;
            for i in range(0, len - gap - 1)
            {
                if arr[i] > arr[i + gap]
                {
                    let swapped = true;
                    let temp = arr[i];
                    let arr[i] = arr[i + gap];
                    let arr[i + gap] = temp;
                }
            }
        }

        echo count(arr);

        for z in range(1, size - 1)
        {
            if arr[z] < arr[z - 1]
            {
                echo "!";
            }
        }
    }

    protected static function _newGap(int! gap) -> int
    {
        //var tmp1 =  gap / 1.3;
        int tmp = (int)(gap / 1.3);

        if tmp == 0
        {
            return 1;
        }

        if tmp == 9 || tmp == 10
        {
            return 11;
        }

        return tmp;
    }
}

Here is where I call it:

<?php
// test file

// http://kokizzu.blogspot.be/2015/02/numeric-combsort-benchmark-updated.html

\Algo\CombSort::launch(10000);

And here is the competing PHP file:

<?php
// http://kokizzu.blogspot.be/2015/02/numeric-combsort-benchmark-updated.html

 //////////////////////////////
   // comb.php
function newGap($gap){
    $gap /= 1.3;
    $gap = (int)$gap;
    if ($gap == 0)
        return 1;
    if($gap == 9 || $gap == 10)
        return 11;
    return $gap;
}
function combSort(&$a) {
    $len = count($a);
    $gap = $len;
    $temp = 0.0;
    $swapped = false;
    while($gap > 1 || $swapped) {
        $swapped = false;
        $gap = newGap($gap);
        for($i=0; $i < $len-$gap; ++$i) {
            if($a[$i] > $a[$i+$gap]) {
                $swapped = true;
                $temp = $a[$i];
                $a[$i] = $a[$i+$gap];
                $a[$i+$gap] = $temp;
            }
        }
    }
}
const N = 10000;
$arr = [];//range(N,0);
for($z=0;$z<N;++$z) $arr[] = N-$z;
combSort($arr);
for($z=1;$z<N;++$z)
{
    if($arr[$z]<$arr[$z-1])
        echo "!";
}

I used a Windows PowerShell tool for getting the benchmark results.

edited Mar '16

I tested your code on Vagrant Box runnin on Ubuntu 14.04, i5 2500k(2 cores on VM) on 4.2 ghz and 4gb ram, php7, zephir 0.9.2a, and for N = 100 000 results mine are:

Zephir : 0.071382999420166 PHP 7 : 0.13500714302063

So its two times faster than php7, i guess on php 5 it would be much more. Oh and test were done on apache2 mpm-fork using mod_php

Also you can speed up php version by taking $len-$gap out of for