3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class SingletonUtil { /** * @var mixed[] * Format: $[$key] =& $variable */ private static $variables = []; /** * @var mixed[] */ private static $defaults = []; /** * @param string $key * @param mixed $variable * @param mixed $value * @param mixed|null $default * Default value to be used when resetting. * * @return mixed */ public static function init($key, &$variable, $value, $default = NULL) { self::$variables[$key] =& $variable; self::$defaults[$key] = $default; return $variable = $value; } /** * @param string ($key_or_null */ public static function reset($key_or_null = NULL) { if (NULL === $key_or_null) { foreach (self::$variables as $key => $variable) { self::$variables[$key_or_null] = self::$defaults[$key_or_null]; } } elseif (array_key_exists($key_or_null, self::$variables)) { self::$variables[$key_or_null] = self::$defaults[$key_or_null]; } } } class C { function __construct() { print __METHOD__ . "()\n"; } } /** * @return mixed */ function singleton_test() { // This would otherwise use drupal_static_fast pattern. // But this is even faster. static $cache; return $cache ?: SingletonUtil::init( __FUNCTION__, $cache, new C()); } function assert_or_ok($boolval) { if (!$boolval) { assert(FALSE); exit(); } print "ok\n"; } $obj = singleton_test(); assert_or_ok($obj instanceof C); assert_or_ok($obj === singleton_test()); SingletonUtil::reset('singleton_test'); assert_or_ok($obj !== singleton_test()); $obj = singleton_test(); assert_or_ok($obj instanceof C); assert_or_ok($obj === singleton_test()); SingletonUtil::reset('singleton_test'); assert_or_ok($obj !== singleton_test());

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.60.0140.00418.31
8.3.50.0140.00018.02
8.3.40.0090.00618.80
8.3.30.0120.00318.88
8.3.20.0080.00020.44
8.3.10.0000.00723.65
8.3.00.0040.00422.39
8.2.180.0030.01316.50
8.2.170.0110.00422.96
8.2.160.0100.00322.16
8.2.150.0050.00324.18
8.2.140.0050.00324.66
8.2.130.0050.00326.16
8.2.120.0000.00719.95
8.2.110.0030.00620.38
8.2.100.0060.00617.84
8.2.90.0000.00817.97
8.2.80.0040.00419.30
8.2.70.0000.00917.63
8.2.60.0050.00317.88
8.2.50.0050.00318.03
8.2.40.0030.00619.91
8.2.30.0000.00817.78
8.2.20.0040.00418.28
8.2.10.0030.00519.30
8.2.00.0040.00418.19
8.1.280.0090.00625.92
8.1.270.0040.00422.06
8.1.260.0040.00426.35
8.1.250.0050.00228.09
8.1.240.0040.00723.81
8.1.230.0060.00620.96
8.1.220.0000.00817.74
8.1.210.0040.00418.78
8.1.200.0080.00017.66
8.1.190.0000.00817.36
8.1.180.0080.00018.10
8.1.170.0060.00317.62
8.1.160.0050.00319.06
8.1.150.0040.00418.84
8.1.140.0040.00420.27
8.1.130.0060.00320.15
8.1.120.0070.00017.43
8.1.110.0040.00417.43
8.1.100.0040.00417.46
8.1.90.0000.00817.36
8.1.80.0070.00017.49
8.1.70.0040.00417.36
8.1.60.0030.00617.59
8.1.50.0080.00017.43
8.1.40.0050.00317.57
8.1.30.0080.00017.59
8.1.20.0030.00617.66
8.1.10.0000.00717.55
8.1.00.0040.00417.50
8.0.300.0000.00719.75
8.0.290.0000.00716.75
8.0.280.0000.00718.54
8.0.270.0040.00417.89
8.0.260.0000.00719.96
8.0.250.0030.00516.93
8.0.240.0000.00717.02
8.0.230.0040.00416.97
8.0.220.0030.00316.89
8.0.210.0030.00316.93
8.0.200.0040.00416.98
8.0.190.0080.00016.95
8.0.180.0030.00616.81
8.0.170.0030.00616.92
8.0.160.0020.00516.77
8.0.150.0050.00316.80
8.0.140.0030.00516.81
8.0.130.0030.00313.36
8.0.120.0050.00317.04
8.0.110.0060.00316.94
8.0.100.0000.00716.91
8.0.90.0050.00216.75
8.0.80.0130.00016.96
8.0.70.0000.00816.93
8.0.60.0080.00016.86
8.0.50.0040.00416.83
8.0.30.0050.01117.22
8.0.20.0060.01617.40
8.0.10.0040.00417.13
8.0.00.0110.00716.84
7.4.330.0000.00515.55
7.4.320.0000.00616.67
7.4.300.0000.00616.52
7.4.290.0030.00316.61
7.4.280.0030.00516.51
7.4.270.0050.00316.47
7.4.260.0030.00316.49
7.4.250.0000.00716.62
7.4.240.0030.00316.46
7.4.230.0070.00016.68
7.4.220.0080.00016.71
7.4.210.0060.01216.56
7.4.200.0000.00716.43
7.4.160.0080.01016.58
7.4.140.0130.00717.86
7.4.130.0060.01616.54
7.4.120.0070.01316.55
7.4.110.0100.01016.52
7.4.100.0080.01616.60
7.4.90.0120.00616.50
7.4.80.0030.01519.39
7.4.70.0070.01016.42
7.4.60.0030.01316.40
7.4.50.0130.00516.38
7.4.40.0060.01816.56
7.4.00.0120.00615.23
7.3.330.0030.00313.42
7.3.320.0000.00513.36
7.3.310.0040.00416.30
7.3.300.0030.00316.45
7.3.290.0030.00316.49
7.3.280.0090.00916.45
7.3.260.0110.01016.42
7.3.240.0100.00816.60
7.3.230.0080.00816.38
7.3.210.0140.00316.50
7.3.200.0110.00716.30
7.3.190.0030.01716.49
7.3.180.0070.01316.50
7.3.170.0100.01016.56
7.3.160.0070.01016.55
7.3.30.0100.00414.71
7.3.20.0590.00814.79
7.3.10.6930.00814.86
7.3.00.3990.00514.69
7.2.330.0090.00916.54
7.2.320.0030.01316.84
7.2.310.0130.00316.91
7.2.300.0030.01416.87
7.2.290.0060.01216.50
7.2.160.0100.01014.82
7.2.150.1120.00515.11
7.2.140.4900.00515.08
7.2.130.0640.00514.97
7.2.120.4860.00714.95
7.2.110.2660.01014.97
7.2.100.2790.00715.09
7.2.90.3580.01015.09
7.2.80.3590.00815.08
7.2.70.2090.00514.77
7.2.60.0690.00815.10
7.2.50.1490.01014.96
7.2.40.0960.00915.06
7.2.30.0770.00314.99
7.2.20.1170.01014.98
7.2.10.0730.00615.09
7.2.00.0830.00715.12
7.1.270.1360.00513.83
7.1.260.4570.00813.85
7.1.250.0720.01013.85

preferences:
58.16 ms | 400 KiB | 5 Q