Created
June 11, 2021 08:09
-
-
Save kobus1998/ad1b5d81a12a6e531498bc4b76f53600 to your computer and use it in GitHub Desktop.
overhead performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class mod { | |
public function __construct(private $aMod) {} | |
public function map($cCall) { | |
$this->aMod = array_map($cCall, $this->aMod); | |
return $this; | |
} | |
public function unique() { | |
$this->aMod = array_unique($this->aMod); | |
return $this; | |
} | |
public function filter() { | |
$this->aMod = array_filter($this->aMod); | |
return $this; | |
} | |
} | |
function test1 ($a) { | |
(new mod($a))->map(function ($b) { | |
return $b; | |
})->unique()->filter(); | |
} | |
function test2($a) { | |
$a = array_filter( | |
array_unique( | |
array_map(function ($b) {return $b;}, $a) | |
) | |
); | |
} | |
$iTests = 10000; | |
$a = range(0, 500); | |
$aResult = []; | |
for ($i = 0; $i < $iTests; $i++) { | |
$fStart = microtime(true); | |
test1($a); | |
$aResult[1][] = round(microtime(true) - $fStart, 9); | |
} | |
for ($i = 0; $i < $iTests; $i++) { | |
$fStart = microtime(true); | |
test2($a); | |
$aResult[2][] = round(microtime(true) - $fStart, 9); | |
} | |
foreach($aResult as $iTest => $aTimes) { | |
echo "{$iTest} => " . (array_sum($aTimes) / count($aTimes)) . "\n"; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment