Page MenuHomePhabricator
Paste P50598

A quick benchmark for checking existence of Value node.
ActivePublic

Authored by pmiazga on Aug 17 2023, 6:51 PM.
Tags
None
Referenced Files
F37551925: A quick benchmark for checking existence of Value node.
Aug 17 2023, 7:29 PM
F37551238: A quick benchmark for checking existence of Value node.
Aug 17 2023, 6:51 PM
Subscribers
None
<?php
interface ValueNodeInterface { };
class BaseNode { };
class ValueNode extends BaseNode {
public $value;
public function __construct($value) { $this->value = $value; }
}
class UnitNode extends BaseNode {
public $unit;
public function __construct($unit) { $this->unit = $unit; }
}
class DeepNode extends UnitNode {
public $deep;
public function __construct( $unit, $deep ) {
$this->deep = $deep;
parent::__construct( $unit );
}
}
class ValueWithInteface extends BaseNode implements ValueNodeInterface {
public $value;
public function __construct($value) { $this->value = $value; }
}
class ValueWithProperty extends BaseNode {
public $hasValue = true;
}
class ValueWithMethod extends BaseNode {
public $value;
public function __construct($value) { $this->value = $value; }
public function hasValue() { return true; }
}
$valueNode = new ValueNode('test');
$unitNode = new UnitNode('unit');
$deepNode = new DeepNode('a', 'deep');
$valueNodeWithMethod = new ValueWithMethod('test');
$valueNodeWithInterface = new ValueWithInteface('test');
$valueNodeWithProperty = new ValueWithProperty('test');
/**
docker run --rm -it -v $(pwd):/opt php:8.2 php /opt/benchmark.php
*/
function benchmarkCall( $name, $node, $callback) {
$runs = [];
echo sprintf("Testing %-42s [", $name);
for($j = 0; $j < 15; $j++ ) {
$before = microtime(true);
for ( $i = 0; $i < 42000000; $i ++ ) {
$callback($node);
}
$after = microtime( true );
$runs[] = $after - $before;
echo '.';
}
echo "] Results - ";
$avg = array_sum($runs) / count($runs);
echo sprintf("avg: %0.10fs, min: %0.10fs, max: %0.10fs \n", $avg, min($runs), max($runs));
}
benchmarkCall('$hasValue property access', $valueNodeWithProperty, function( $node ) {
return $node->hasValue;
});
benchmarkCall('?? with not ValueNode', $deepNode, function( $node ) {
return $node->value ?? null;
});
benchmarkCall('?? with ValueNode', $valueNode, function( $node ) {
return $node->value ?? null;
});
benchmarkCall('Call property_exists() with ValueNode', $valueNode, function( $node ) {
return property_exists($node, 'value');
});
benchmarkCall('Call property_exists() with not ValueNode', $unitNode, function( $node ) {
return property_exists($node, 'value');
});
benchmarkCall('Instanceof with node that implements', $valueNodeWithInterface, function( $node ) {
return $node instanceof ValueNodeInterface;
});
benchmarkCall('instanceof with node that do not implement', $deepNode, function( $node ) {
return $node instanceof ValueNodeInterface;
});
benchmarkCall('isset with not ValueNode', $deepNode, function( $node ) {
return isset($node->value);
});
benchmarkCall('isset with ValueNode', $valueNode, function( $node ) {
return isset($node->value);
});
benchmarkCall('method hasValue that returns bool', $valueNodeWithMethod, function( $node ) {
return $node->hasValue();
});