3v4l.org

run code in 300+ PHP versions simultaneously
<?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();
Output for 8.0.9
Total execution time for 5000 iterations for BadActor = 0.277042 milliseconds Total execution time for 5000 iterations for GoodActor = 0.790119 milliseconds Total time difference for 5000 iterations = 0.513077 milliseconds Total time difference for 1 iteration = 1/9,745 millisecond
Output for 7.4.23
Total execution time for 5000 iterations for BadActor = 0.304937 milliseconds Total execution time for 5000 iterations for GoodActor = 0.767946 milliseconds Total time difference for 5000 iterations = 0.463009 milliseconds Total time difference for 1 iteration = 1/10,799 millisecond
Output for 7.3.30
Parse error: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/HAkhjW on line 8
Process exited with code 255.

preferences:
54.86 ms | 401 KiB | 8 Q