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) ?>

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).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.70.0520.33956.60
8.3.60.0590.33256.48
8.3.50.0400.35956.41
8.3.40.0340.10140.16
8.3.30.0300.11240.17
8.3.20.0320.10040.20
8.3.10.0370.09340.04
8.3.00.0260.10540.33
8.2.190.0460.34256.62
8.2.180.0580.34056.53
8.2.170.0330.09240.68
8.2.160.0410.09140.77
8.2.150.0310.10140.60
8.2.140.0270.10340.63
8.2.130.0330.09640.58
8.2.120.0350.09740.16
8.2.110.0340.10440.06
8.2.100.0270.10840.44
8.2.90.0240.10740.47
8.2.80.0280.10340.45
8.2.70.0380.09240.46
8.2.60.0330.10040.45
8.2.50.0390.11640.54
8.2.40.0300.10540.50
8.2.30.0450.08340.32
8.2.20.0300.09640.27
8.2.10.0270.09940.40
8.2.00.0440.09840.26
8.1.280.0670.39164.20
8.1.270.0500.09144.00
8.1.260.0370.10344.14
8.1.250.0330.10744.51
8.1.240.0370.09344.34
8.1.230.0340.11244.48
8.1.220.0210.11044.29
8.1.210.0300.10444.17
8.1.200.0440.09244.27
8.1.190.0570.10143.84
8.1.180.0540.08543.94
8.1.170.0440.11643.96
8.1.160.0460.10643.95
8.1.150.0450.09644.50
8.1.140.0370.11244.30
8.1.130.0320.10744.37
8.1.120.0410.10244.38
8.1.110.0240.12444.21
8.1.100.0270.10544.38
8.1.90.0370.09644.12
8.1.80.0460.11144.30
8.1.70.0440.11144.25
8.1.60.0480.09244.25
8.1.50.0500.10344.19
8.1.40.0400.09943.96
8.1.30.0370.11044.18
8.1.20.0490.09244.06
8.1.10.0570.42465.45
8.1.00.0760.40465.32
8.0.300.0820.32964.39
8.0.290.0630.36664.64
8.0.280.0580.35264.50
8.0.270.0800.34764.38
8.0.260.0740.35464.75
8.0.250.0800.34064.80
8.0.240.0470.36164.93
8.0.230.0660.36364.55
8.0.220.0800.37764.55
8.0.210.0530.35464.40
8.0.200.0860.35864.64
8.0.190.0620.35764.71
8.0.180.0710.36064.91
8.0.170.0860.33064.63
8.0.160.0630.36364.50
8.0.150.0510.36264.36
8.0.140.0580.41164.45
8.0.130.0580.41864.45
8.0.120.0560.42264.70
8.0.110.0460.48764.63
8.0.100.0620.46264.49
8.0.90.0590.42864.69
8.0.80.0600.41664.54
8.0.70.0600.43164.54
8.0.60.0570.41764.78
8.0.50.0540.42964.70
8.0.30.0520.42764.47
8.0.20.0740.50764.74
8.0.10.0780.53864.72
8.0.00.0630.56164.77
7.4.330.0540.40564.27
7.4.320.0700.36964.54
7.4.300.0660.39964.11
7.4.290.0720.36564.48
7.4.280.0770.39064.15
7.4.270.0540.45364.35
7.4.260.0520.46764.36
7.4.250.0590.45464.22
7.4.240.0580.48064.03
7.4.230.0600.45364.25
7.4.220.0560.46464.34
7.4.210.0680.44564.27
7.4.200.0530.47964.35
7.4.190.0610.45064.41
7.4.180.0630.44964.33
7.4.160.0560.46664.44
7.4.150.0650.55464.19
7.4.140.0800.56163.96
7.4.130.0600.57364.24
7.4.120.0800.55764.31
7.4.110.0720.57264.09
7.4.100.0640.62764.29
7.4.90.0800.63164.22
7.4.80.0760.62864.11
7.4.70.0910.71664.18
7.4.60.0800.63664.21
7.4.50.0950.69564.21
7.4.40.0920.72864.12
7.4.30.0830.63464.21
7.4.20.0760.65364.19
7.4.10.0690.68764.17
7.4.00.0790.66364.15
7.3.330.0580.44864.14
7.3.320.0600.51762.57
7.3.310.0570.45764.02
7.3.300.0590.45763.92
7.3.290.0530.46264.07
7.3.280.0490.48964.04
7.3.270.0680.62564.11
7.3.260.0550.58064.07
7.3.250.0780.55764.16
7.3.240.0720.57064.12
7.3.230.0640.59364.05
7.3.220.0710.62364.06
7.3.210.0730.65164.00
7.3.200.0830.65264.11
7.3.190.0810.66564.18
7.3.180.0810.66064.16
7.3.170.1010.73964.14
7.3.160.0820.79764.08
7.3.150.0960.64364.18
7.3.140.0770.62464.17
7.3.130.0800.72164.20
7.3.120.0790.67964.14
7.3.110.0910.77564.08
7.3.100.0810.76164.03
7.3.90.0790.70664.14
7.3.80.0970.76964.08
7.3.70.0790.68264.12
7.3.60.0760.67764.13
7.3.50.0750.67764.19
7.3.40.0740.70664.13
7.3.30.0660.71064.19
7.3.20.1310.72564.24
7.3.10.1220.68464.65
7.3.00.1110.76164.65
7.2.340.0760.68964.06
7.2.330.0910.78364.09
7.2.320.0700.81863.98
7.2.310.0770.73863.96
7.2.300.0870.86964.06
7.2.290.0930.84463.91
7.2.280.0740.78464.12
7.2.270.0830.81364.05
7.2.260.0740.88563.99
7.2.250.0770.83464.07
7.2.240.0790.79764.09
7.2.230.0840.80364.01
7.2.220.0830.85864.04
7.2.210.0730.78664.07
7.2.200.0850.78164.14
7.2.190.0730.80864.08
7.2.180.0860.77864.02
7.2.170.0680.79664.18
7.2.160.0690.81164.13
7.2.150.1220.84664.62
7.2.140.1010.78264.67
7.2.130.0960.80664.72
7.2.120.1070.83264.65
7.2.110.1020.79964.64
7.2.100.1150.78164.58
7.2.90.0990.78464.59
7.2.80.1080.79164.63
7.2.70.1000.79864.67
7.2.60.1080.80164.66
7.2.50.1130.82964.60
7.2.40.1230.95164.56
7.2.30.1080.85864.77
7.2.20.1040.92164.73
7.2.10.1040.84464.56
7.2.00.1070.84264.72

preferences:
28.4 ms | 403 KiB | 5 Q