Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created June 11, 2021 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kobus1998/ad1b5d81a12a6e531498bc4b76f53600 to your computer and use it in GitHub Desktop.
Save kobus1998/ad1b5d81a12a6e531498bc4b76f53600 to your computer and use it in GitHub Desktop.
overhead performance test
<?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