3v4l.org

run code in 300+ PHP versions simultaneously
<?php set_time_limit(0); Class Static0 { public static $counter = 0; public static function incrByTwo() { self::$counter += 2; } } Class Static1 extends Static0 { public static function incrByOne() { self::$counter++; } public static function incrByTwo() { parent::incrByTwo(); } } Class Dynamic0 { public $counter = 0; public function incrByTwo() { $this->counter += 2; } } Class Dynamic1 extends Dynamic0 { public function incrByOne() { $this->counter++; } public function incrByTwo() { parent::incrByTwo(); } } Class Singleton0 { private static $instances = []; public static function getInstance() { $self = static::class; if (!isset(self::$instances[$self])) { self::$instances[$self] = new $self; } return self::$instances[$self]; } final private function __construct() { } final private function __clone() { } protected function __wakeup() { } public $counter = 0; public function incrByTwo() { $this->counter += 2; } } Class Singleton1 extends Singleton0 { public function incrByOne() { $this->counter++; } public function incrByTwo() { parent::incrByTwo(); } } function incrByOne_by_ref(&$counter) { $counter++; } function parent_incrByTwo_by_ref(&$counter) { $counter += 2; } function incrByTwo_by_ref(&$counter) { parent_incrByTwo_by_ref($counter); } function incrByOne_global() { global $counter; $counter++; } function parent_incrByTwo_global() { global $counter; $counter += 2; } function incrByTwo_global() { parent_incrByTwo_global(); } function incrByOne_globals() { $GLOBALS['counter']++; } function parent_incrByTwo_globals() { $GLOBALS['counter'] += 2; } function incrByTwo_globals() { parent_incrByTwo_globals(); } abstract class MyStorage { public static $counter = 0; } function incrByOne_static_prop() { MyStorage::$counter++; } function parent_incrByTwo_static_prop() { MyStorage::$counter += 2; } function incrByTwo_static_prop() { parent_incrByTwo_static_prop(); } // free runs so everybody is well loaded $runs=10; for($i=0;$i<$runs;$i++) { $obj = new Dynamic1(); $obj->incrByOne(); $obj->incrByTwo(); } for($i=0;$i<$runs;$i++) { Static1::incrByOne(); Static1::incrByTwo(); } unset($obj, $i); $runs=500000; $memories = [memory_get_usage()]; ?> Runs by case: <?= $runs ?> Memory usage at start: <?= number_format(end($memories)) ?> <?php $time_start = microtime(true); $obj = new Dynamic1(); for($i=0;$i<$runs;$i++) { $obj->incrByOne(); $obj->incrByTwo(); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Dynamic Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $time_start = microtime(true); for($i=0;$i<$runs;$i++) { $obj = new Dynamic1(); $obj->incrByOne(); $obj->incrByTwo(); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Dynamic instantiated Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $storage = []; $time_start = microtime(true); for($i=0;$i<$runs;$i++) { $obj = new Dynamic1(); $obj->incrByOne(); $obj->incrByTwo(); $storage[] = $obj; } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; unset($storage); $memories[] = memory_get_usage(); ?> + Dynamic instantiated stored Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $storage2 = []; $obj = new Dynamic1(); $time_start = microtime(true); for($i=0;$i<$runs;$i++) { $storage2[] = $obj; } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; unset($storage2); $memories[] = memory_get_usage(); ?> + Storage only Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $time_start = microtime(true); for($i=0;$i<$runs;$i++) { Static1::incrByOne(); Static1::incrByTwo(); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Static Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $time_start = microtime(true); for($i=0;$i<$runs;$i++) { $obj = Singleton1::getInstance(); $obj->incrByOne(); $obj->incrByTwo(); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Singletons with many getInstance Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $time_start = microtime(true); $obj = Singleton1::getInstance(); for($i=0;$i<$runs;$i++) { $obj->incrByOne(); $obj->incrByTwo(); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Singletons with one getInstance Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $counter = 0; $time_start = microtime(true); for($i=0;$i<$runs;$i++) { incrByOne_globals(); incrByTwo_globals(); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Functions with $GLOBALS Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $counter = 0; $time_start = microtime(true); for($i=0;$i<$runs;$i++) { incrByOne_global(); incrByTwo_global(); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Functions with global Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php $counter = 0; $time_start = microtime(true); for($i=0;$i<$runs;$i++) { incrByOne_by_ref($counter); incrByTwo_by_ref($counter); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Functions with $counter passed by ref Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?> <?php MyStorage::$counter = 0; $time_start = microtime(true); for($i=0;$i<$runs;$i++) { incrByOne_static_prop($counter); incrByTwo_static_prop($counter); } $time_end = microtime(true); $time1 = $time_end - $time_start; $last_memory = end($memories); $memories[] = memory_get_usage(); $memory_diff = end($memories) - $last_memory; ?> + Functions with $counter stored in a static property Total execution time is <?= number_format($time1, 4) ?> Used memory: <?= number_format($memory_diff) ?>

preferences:
46.27 ms | 409 KiB | 5 Q