@ 2021-08-26T07:22:58Z <?php
const ITERATIONS = 5000;
interface Actor {}
class InnocentBystander {
public int $value;
public Actor $actor;
}
class GoodActor implements Actor {
private array $subclasses = array();
public function __get(string $subclass)
{
if (isset($this->subclasses[$subclass])) {
return $this->subclasses[$subclass];
}
$this->subclasses[$subclass] = new $subclass;
$this->subclasses[$subclass]->actor = $this;
return $this->subclasses[$subclass];
}
}
class BadActor implements Actor {
public function __get(string $subclass)
{
$this->$subclass = new $subclass;
$this->$subclass->actor = $this;
return $this->$subclass;
}
}
function time_it(Actor $instance, callable $callable):float {
$start = microtime( true );
for ( $i = 0; $i < ITERATIONS; $i ++ ) {
$callable($instance, $i);
}
$time = ( microtime( true ) - $start )*1000;
printf( "Total execution time for %d iterations for %s = %f milliseconds\n" ,
ITERATIONS,
get_class($instance),
$time );
return $time;
}
function main() {
$callable = function($actor, $number) {
$actor->InnocentBystander->value = $number;
};
$bad_timed = time_it( new BadActor,$callable );
$good_timed = time_it( new GoodActor,$callable );
$delta = $good_timed - $bad_timed;
printf( "Total time difference for %d iterations = %f milliseconds\n" ,
ITERATIONS,
$delta);
printf( "Total time difference for 1 iteration = 1/%s millisecond\n" ,
number_format(ITERATIONS/$delta,0));
}
main();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.0.9 0.003 0.007 17.00 7.4.23 0.004 0.005 16.49 7.3.30 0.007 0.000 16.13
preferences:dark mode live preview
36.46 ms | 401 KiB | 5 Q