3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hashcode($str){ // return sha1($str); return $str; } class SomethingBuilder { private $name; private $super; function __construct($name){ $this->name = $name; } function getName(){ return $this->name; } function setSuper(SomethingBuilder $super = null){ $this->super = $super; } function getSuper(){ return $this->super; } function getHash(){ return hashcode(spl_object_hash($this)); } function equals($other){ return $this->getHash() === $other->getHash(); } } class Something { protected $super; protected $name; function __construct(SomethingBuilder $builder){ $this->name = $builder->getName(); $argBuilder = $builder; $immutables = []; $builders = []; do{ if(isset($immutables[$builder->getHash()])) break; // this should rather be a private class, which php doesn't have yet, but better than nothing. $immutable = $builder === $argBuilder ? $this : new class($builder->getName()) extends Something{ function __construct($name){ $this->name = $name; } }; $immutables[$builder->getHash()] = $immutable; $builders[$builder->getHash()] = $builder; }while($builder = $builder->getSuper()); foreach($immutables as $builderHash => $immutable){ $immutable->super = $immutables[$builders[$builderHash]->getSuper()->getHash()]; } } function getName(){ return $this->name; } function getSuper(){ return $this->super; } function getHash($origin = null){ if($this === $origin){ return hashcode("*RECURSION*"); }else{ return hashcode( "Something" . "{" . $this->getName() . "|" . $this->getSuper()->getHash($origin ?? $this) . "}" ); } } function equals($other){ return $this->getHash() === $other->getHash(); } } $a = new SomethingBuilder("A"); $b = new SomethingBuilder("B"); $c = new SomethingBuilder("C"); $a->setSuper($c); $b->setSuper($a); $c->setSuper($b); $immutable = new Something($a); var_dump($immutable->getHash()); var_dump($immutable->getSuper()->getHash()); var_dump($immutable->getSuper()->getSuper()->getHash()); var_dump($immutable->getSuper()->getSuper()->getSuper()->getHash()); var_dump($immutable->getSuper()->getSuper()->getSuper()->getSuper()->getHash()); assert($immutable === $immutable->getSuper()->getSuper()->getSuper()); assert( $immutable->equals( $immutable->getSuper()->getSuper()->getSuper() ) ); echo "\n\n"; $a = new SomethingBuilder("A"); $a->setSuper($a); $immutable = new Something($a); var_dump($immutable->getHash()); var_dump($immutable->getSuper()->getHash()); assert($immutable === $immutable->getSuper()); assert($immutable->equals($immutable->getSuper()));

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.0070.00718.30
8.3.50.0090.01221.17
8.3.40.0120.00318.79
8.3.30.0060.00918.79
8.3.20.0120.00320.16
8.3.10.0070.00023.70
8.3.00.0050.00322.44
8.2.180.0160.00316.75
8.2.170.0150.00622.96
8.2.160.0070.00720.50
8.2.150.0030.00524.18
8.2.140.0030.00624.66
8.2.130.0060.00326.16
8.2.120.0060.00317.88
8.2.110.0000.00920.66
8.2.100.0040.00717.97
8.2.90.0000.00819.48
8.2.80.0030.00617.97
8.2.70.0030.00617.88
8.2.60.0060.00318.05
8.2.50.0050.00318.07
8.2.40.0030.00521.17
8.2.30.0060.00319.79
8.2.20.0040.00417.95
8.2.10.0080.00018.22
8.2.00.0080.00018.32
8.1.280.0120.00625.92
8.1.270.0000.00820.50
8.1.260.0060.00326.35
8.1.250.0000.00828.09
8.1.240.0000.01023.77
8.1.230.0060.00620.99
8.1.220.0060.00317.78
8.1.210.0080.00018.77
8.1.200.0000.01017.48
8.1.190.0050.00317.48
8.1.180.0030.00518.10
8.1.170.0040.00418.68
8.1.160.0000.00822.13
8.1.150.0040.00419.00
8.1.140.0030.00617.46
8.1.130.0040.00417.79
8.1.120.0000.00717.47
8.1.110.0030.00517.46
8.1.100.0040.00417.56
8.1.90.0040.00417.59
8.1.80.0000.00817.45
8.1.70.0000.00717.59
8.1.60.0060.00317.57
8.1.50.0000.00817.53
8.1.40.0040.00717.50
8.1.30.0030.00617.69
8.1.20.0000.00817.63
8.1.10.0050.00317.71
8.1.00.0070.00017.59
8.0.300.0000.00818.77
8.0.290.0070.00016.63
8.0.280.0080.00018.52
8.0.270.0000.00817.28
8.0.260.0070.00017.43
8.0.250.0040.00417.01
8.0.240.0000.00717.12
8.0.230.0070.00017.12
8.0.220.0030.00317.05
8.0.210.0030.00417.07
8.0.200.0070.00017.10
8.0.190.0030.00917.13
8.0.180.0080.00016.98
8.0.170.0040.00416.95
8.0.160.0000.00717.00
8.0.150.0000.00716.91
8.0.140.0000.00716.93
8.0.130.0030.00313.48
8.0.120.0040.00417.04
8.0.110.0040.00416.98
8.0.100.0040.00417.07
8.0.90.0030.00517.12
8.0.80.0110.00417.10
8.0.70.0000.00717.13
8.0.60.0050.00216.94
8.0.50.0080.00016.95
8.0.30.0130.00417.06
8.0.20.0090.01317.40
8.0.10.0040.00417.24
8.0.00.0090.01016.94
7.4.330.0000.00515.08
7.4.320.0030.00316.64
7.4.300.0030.00316.61
7.4.290.0040.00416.59
7.4.280.0070.00016.66
7.4.270.0000.00716.75
7.4.260.0050.00216.54
7.4.250.0080.00016.54
7.4.240.0040.00416.59
7.4.230.0070.00016.52
7.4.220.0080.01116.59
7.4.210.0110.00316.80
7.4.200.0030.00316.44
7.4.160.0090.00616.46
7.4.150.0090.00917.40
7.4.140.0110.01017.86
7.4.130.0170.00516.62
7.4.120.0090.01216.63
7.4.110.0090.00916.47
7.4.100.0110.00816.59
7.4.90.0110.00716.53
7.4.80.0190.00619.39
7.4.70.0000.01716.75
7.4.60.0100.00616.61
7.4.50.0050.00316.48
7.4.40.0100.00716.38
7.4.30.0180.00616.70
7.4.00.0070.01015.27
7.3.330.0030.00313.53
7.3.320.0030.00313.36
7.3.310.0040.00416.40
7.3.300.0030.00316.38
7.3.290.0140.00116.45
7.3.280.0100.01016.49
7.3.270.0140.00717.40
7.3.260.0090.01216.71
7.3.250.0080.01016.49
7.3.240.0110.01016.73
7.3.230.0140.00316.42
7.3.210.0130.00416.53
7.3.200.0090.00916.46
7.3.190.0060.01216.71
7.3.180.0100.00716.45
7.3.170.0070.01016.54
7.3.160.0110.00716.51
7.2.330.0070.01016.92
7.2.320.0110.00716.83
7.2.310.0070.01416.78
7.2.300.0120.00616.78
7.2.290.0100.00616.62
7.2.60.0110.00317.10
7.2.00.0060.00619.57
7.1.200.0030.01015.94
7.1.100.0150.00418.34
7.1.70.0100.00017.20
7.1.60.0090.01520.03
7.1.50.0210.01116.82
7.1.00.0000.08022.36
7.0.200.0400.01014.97
7.0.60.0170.07720.08
7.0.50.0130.03017.97
7.0.40.0100.08020.17
7.0.30.0230.04020.13
7.0.20.0330.07020.10
7.0.10.0130.08320.33
7.0.00.0030.08720.22
5.6.280.0030.05320.91
5.6.210.0070.07020.61
5.6.200.0170.07718.18
5.6.190.0030.04320.48
5.6.180.0370.06720.47
5.6.170.0330.04320.50
5.6.160.0130.07320.54
5.6.150.0100.07318.28
5.6.140.0100.07318.15
5.6.130.0100.08018.20
5.6.120.0170.08021.04
5.6.110.0230.07021.03
5.6.100.0070.07321.04
5.6.90.0130.07721.11
5.6.80.0170.06720.52
5.5.350.0070.07320.42
5.5.340.0100.07318.05
5.5.330.0070.08320.08
5.5.320.0000.08320.40
5.5.310.0130.04720.25
5.5.300.0070.05018.08
5.5.290.0170.06718.07
5.5.280.0070.04720.66
5.5.270.0130.07320.89
5.5.260.0100.08020.82
5.5.250.0100.05320.70
5.5.240.0100.03020.05
5.4.450.0700.07319.32
5.4.440.0930.06319.58
5.4.430.0770.05319.30
5.4.420.0700.05319.38
5.4.410.0700.05019.49
5.4.400.0800.06019.38
5.4.390.0730.05319.08
5.4.380.0770.06019.14
5.4.370.0830.04319.14
5.4.360.0670.05719.14
5.4.350.0700.06319.15
5.4.340.0630.05719.16
5.4.320.0800.07019.13
5.4.310.0800.05319.15
5.4.300.0600.05719.05
5.4.290.0530.05719.06
5.4.280.0100.05719.14
5.4.270.0170.04719.25
5.4.260.0470.06319.14
5.4.250.0770.06718.84
5.4.240.0670.05319.05
5.4.230.0830.06019.38
5.4.220.0730.05319.22
5.4.210.0870.04719.09
5.4.200.0630.05716.82
5.4.190.0800.05018.98
5.4.180.0730.06019.23
5.4.170.0670.05319.33
5.4.160.0170.04718.90
5.4.150.0700.04718.95
5.4.140.1100.05016.45
5.4.130.0800.04716.59
5.4.120.0770.04716.45
5.4.110.0770.07716.44
5.4.100.0800.05716.40
5.4.90.0700.05316.39
5.4.80.0800.04716.49
5.4.70.0930.06716.32
5.4.60.0070.03316.43
5.4.50.0030.08016.43
5.4.40.0230.07016.54
5.4.30.0200.04016.33
5.4.20.0370.03316.34

preferences:
46.36 ms | 401 KiB | 5 Q