IchHabRecht / mask_export

Export your mask elements as extension
GNU General Public License v2.0
45 stars 27 forks source link

[TASK] Define inline functions static #186

Closed simonschaufi closed 2 years ago

IchHabRecht commented 2 years ago

Hi @simonschaufi,

Can you please explain what issue this pull requests fixes?

IchHabRecht commented 2 years ago

Hi @simonschaufi,

Can you please explain what issue this pull requests fixes?

simonschaufi commented 2 years ago

This is a micro optimization because static functions are slightly faster, which I confirmed myself:

<?php

class SomeClass
{
    public function doTest($i)
    {
    }

    public static function doStaticTest($i)
    {
    }
}

$someObj = new SomeClass();

// bench static method
$starttime = microtime(true);
for ($i = 0; $i< 100*1000*1000; $i++) {
    SomeClass::doStaticTest($i);
}

echo 'Static Time:   ' , (microtime(true)-$starttime) , " ms\n";

// bench object method
$starttime = microtime(true);

for ($i = 0; $i < 100*1000*1000; $i++) {
    $someObj->doTest($i);
}

echo 'Object Time:   ' , (microtime(true)-$starttime) , " ms\n";

// bench non static closure
$starttime = microtime(true);

$myList = [1];
for ($i = 0; $i < 10*1000*1000; $i++) {
    array_map(function ($i) {
        return $i;
    }, $myList);
}

echo 'Non static closure Time: ' , (microtime(true)-$starttime) , " ms\n";

// bench static closure
$starttime = microtime(true);

for ($i = 0; $i < 10*1000*1000; $i++) {
    array_map(static function ($i) {
        return $i;
    }, $myList);
}

echo 'Static closure Time: ' , (microtime(true)-$starttime) , " ms\n";
Static Time:   1.269611120224 ms
Object Time:   1.7090179920197 ms

Non static closure Time: 1.4039039611816 ms
Static closure Time: 1.367977142334 ms
IchHabRecht commented 2 years ago

Unfortunately you missed some fixes.

simonschaufi commented 2 years ago

thanx :partying_face: