Skip to content

Instantly share code, notes, and snippets.

@Isinlor
Created June 22, 2017 08:01
Show Gist options
  • Save Isinlor/e33775543dc4d4871c29fb8808c98850 to your computer and use it in GitHub Desktop.
Save Isinlor/e33775543dc4d4871c29fb8808c98850 to your computer and use it in GitHub Desktop.
/**
* Preliminary benchmark trying to figure out whether arrays or objects are faster in PHP in use cases of the algorithm.
*
* @author Tomasz Darmetko <tomasz.darmetko@gmail.com>
*/
class ObjectVsArrayBench extends AbstractBenchmarkCase
{
use RelevantRelationshipTrait;
public function benchCreateObjectDirectly()
{
$relationship = new Relationship;
$relationship->type = 'element';
}
public function benchCreateObjectFromArray()
{
$relationship = Relationship::createFromArray(['type' => 'element']);
}
public function benchCreateArray()
{
$relationship = ['type' => 'element'];
}
public function benchGetRelationshipObject()
{
$relationship = new Relationship;
$relationship->type1 = 'element';
$relationship->type2 = 'element';
$relationship->getRelevantRelationship(['type1']);
}
public function benchGetRelationshipArray()
{
$relationship = ['type1' => 'element', 'type2' => 'element'];
$this->getRelevantRelationship($relationship, ['type1']);
}
public function benchGetLongRelationshipObject()
{
$relationship = new Relationship;
$relationship->type1 = 'element';
$relationship->type2 = 'element';
$relationship->type3 = 'element';
$relationship->type4 = 'element';
$relationship->type5 = 'element';
$relationship->type6 = 'element';
$relationship->getRelevantRelationship(['type3', 'type4', 'type5']);
$relationship->getRelevantRelationship(['type3', 'type4', 'type5']);
$relationship->getRelevantRelationship(['type3', 'type4', 'type5']);
$relationship->getRelevantRelationship(['type3', 'type4', 'type5']);
$relationship->getRelevantRelationship(['type3', 'type4', 'type5']);
$relationship->getRelevantRelationship(['type3', 'type4', 'type5']);
}
public function benchGetLongRelationshipArray()
{
$relationship = [
'type1' => 'element',
'type2' => 'element',
'type3' => 'element',
'type4' => 'element',
'type5' => 'element',
'type6' => 'element'
];
$this->getRelevantRelationship($relationship, ['type3', 'type4', 'type5']);
$this->getRelevantRelationship($relationship, ['type3', 'type4', 'type5']);
$this->getRelevantRelationship($relationship, ['type3', 'type4', 'type5']);
$this->getRelevantRelationship($relationship, ['type3', 'type4', 'type5']);
$this->getRelevantRelationship($relationship, ['type3', 'type4', 'type5']);
$this->getRelevantRelationship($relationship, ['type3', 'type4', 'type5']);
}
}
class Relationship
{
public function getRelevantRelationship(array $types)
{
$relevantRelationship = new Relationship;
foreach ($types as $type) {
$relevantRelationship->{$type} = $this->{$type};
}
return $relevantRelationship;
}
public static function createFromArray(array $arrayRelationship = [])
{
$relationship = new Relationship;
foreach ($arrayRelationship as $type => $element) {
$relationship->{$type} = $element;
}
return $relationship;
}
}
PhpBench 0.13.0. Running benchmarks.
Using configuration file: ...
\Benchmarks\ObjectVsArrayBench
benchCreateObjectDirectly R2 I6 P0 [μ Mo]/r: 1.788 1.795 (μs) [μSD μRSD]/r: 0.022μs 1.24%
benchCreateObjectFromArray R3 I8 P0 [μ Mo]/r: 2.916 2.888 (μs) [μSD μRSD]/r: 0.061μs 2.08%
benchCreateArray R2 I9 P0 [μ Mo]/r: 0.960 0.968 (μs) [μSD μRSD]/r: 0.012μs 1.29%
benchGetRelationshipObject R2 I6 P0 [μ Mo]/r: 3.733 3.668 (μs) [μSD μRSD]/r: 0.095μs 2.54%
benchGetRelationshipArray R1 I6 P0 [μ Mo]/r: 2.300 2.286 (μs) [μSD μRSD]/r: 0.045μs 1.96%
benchGetLongRelationshipObjectR1 I0 P0 [μ Mo]/r: 14.539 14.394 (μs) [μSD μRSD]/r: 0.276μs 1.90%
benchGetLongRelationshipArray R1 I1 P0 [μ Mo]/r: 9.625 9.511 (μs) [μSD μRSD]/r: 0.203μs 2.11%
7 subjects, 70 iterations, 70,000 revs, 0 rejects
(best [mean mode] worst) = 0.941 [5.123 5.073] 0.979 (μs)
⅀T: 358.613μs μSD/r 0.102μs μRSD/r: 1.873%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment