3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Comparator { public static function compareEntities($entityX, $entityY, array $exclusion = []) { if (is_object($entityX) && is_object($entityY)) { return self::checkObjectsEquals($entityX, $entityY, $exclusion); } if (is_array($entityX) && is_array($entityY)) { return self::checkArraysEquals($entityX, $entityY, $exclusion); } return $entityX == $entityY; } public static function checkObjectsEquals($objectX, $objectY, array $exclusion = []) { if (!is_object($objectX) || !is_object($objectY)) { return false; } return self::checkObjectsEqualsByReflection($objectX, $objectY, $exclusion); } public static function checkArraysEquals(array $arrayX, array $arrayY, array $exclusion = []) { return self::checkArraysEqualsByReflection($arrayX, $arrayY, $exclusion); } private static function checkObjectsEqualsByReflection($objectX, $objectY, array $exclusion = [], array &$visitedObjectHashes = []) { if (get_class($objectX) != get_class($objectY)) { return false; } $visitedObjectHashes[] = self::getObjectHash($objectX); $visitedObjectHashes[] = self::getObjectHash($objectY); $reflectionX = new \ReflectionObject($objectX); $reflectionY = new \ReflectionObject($objectY); if (count($reflectionX->getProperties()) != count($reflectionY->getProperties())) { return false; } foreach ($reflectionX->getProperties() as $property) { $property->setAccessible(true); if (!$reflectionY->hasProperty($property->getName())) { return false; } if (!in_array($property->getName(), $exclusion)) { $entityX = $property->getValue($objectX); $entityY = $property->getValue($objectY); if (!($type = self::getPairType($entityX, $entityY))) { return false; } switch ($type) { case 'object' : $alreadyVisited = array_intersect([self::getObjectHash($entityX), self::getObjectHash($entityY)], $visitedObjectHashes); if (!$alreadyVisited && !self::checkObjectsEqualsByReflection($entityX, $entityY, $exclusion, $visitedObjectHashes)) { return false; } break; case 'array' : if (!self::checkArraysEqualsByReflection($entityX, $entityY, $exclusion, $visitedObjectHashes)) { return false; } break; default : if ($entityX != $entityY) { return false; } break; } } } return true; } private static function checkArraysEqualsByReflection(array $arrayX, array $arrayY, array $exclusion = [], array &$visitedObjectHashes = []) { if (count($arrayX) != count($arrayY)) { return false; } $entityY = current($arrayY); foreach ($arrayX as $entityX) { if (!($type = self::getPairType($entityX, $entityY))) { return false; } switch ($type) { case 'object' : $alreadyVisited = array_intersect([self::getObjectHash($entityX), self::getObjectHash($entityY)], $visitedObjectHashes); if (!$alreadyVisited && !self::checkObjectsEqualsByReflection($entityX, $entityY, $exclusion, $visitedObjectHashes)) { return false; } break; case 'array' : if (!self::checkArraysEqualsByReflection($entityX, $entityY, $exclusion, $visitedObjectHashes)) { return false; } break; default : if ($entityX != $entityY) { return false; } break; } $entityY = next($arrayY); } return true; } private static function getObjectHash($object) { return spl_object_hash($object); } private static function getPairType($entityX, $entityY) { if (gettype($entityX) != gettype($entityY)) { return null; } return gettype($entityX); } } class Foo { private $holder; private $children; public function __construct($content = '', array $children = []) { $this->holder = $content; $this->setChildren($children); } public function setChildren(array $children) { $this->children = $children; foreach ($children as $child) { $child->setParent($this); } } } class Bar { private $holder; private $parent; public function __construct($content = '', Foo $parent = null) { $this->holder = $content; $this->setParent($parent); } public function setParent(Foo $parent = null) { $this->parent = $parent; } } $child0 = new Bar('child#0'); $child1 = new Bar('child#1'); //same $child2 = new Bar('child#0'); $child3 = new Bar('child#1'); $parent0 = new Foo('parent', [$child0, $child1]); $parent1 = new Foo('parent', [$child2, $child3]); //Valid: var_dump(Comparator::compareEntities($parent0, $parent1));

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.0150.00716.58
8.3.60.0110.01116.75
8.3.50.0130.00823.03
8.3.40.0150.00619.04
8.3.30.0110.00419.35
8.3.20.0040.00420.31
8.3.10.0030.00521.89
8.3.00.0030.00617.93
8.2.190.0040.01816.88
8.2.180.0130.00625.92
8.2.170.0110.01119.12
8.2.160.0140.00422.96
8.2.150.0050.00324.18
8.2.140.0000.00824.66
8.2.130.0080.00026.16
8.2.120.0040.00420.98
8.2.110.0040.00422.11
8.2.100.0030.00917.91
8.2.90.0060.00319.47
8.2.80.0000.00918.05
8.2.70.0050.00317.50
8.2.60.0040.00418.16
8.2.50.0040.00418.07
8.2.40.0060.00319.88
8.2.30.0000.00819.91
8.2.20.0080.00017.91
8.2.10.0040.00418.22
8.2.00.0000.00817.79
8.1.280.0090.00625.92
8.1.270.0050.00324.66
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0060.00323.96
8.1.230.0080.00319.09
8.1.220.0040.00417.91
8.1.210.0070.00518.77
8.1.200.0000.00917.48
8.1.190.0040.00417.66
8.1.180.0060.00318.10
8.1.170.0050.00518.40
8.1.160.0030.00622.18
8.1.150.0000.00718.92
8.1.140.0040.00417.56
8.1.130.0000.00717.98
8.1.120.0000.00717.60
8.1.110.0030.00517.46
8.1.100.0030.00517.42
8.1.90.0040.00417.50
8.1.80.0040.00417.47
8.1.70.0000.00717.46
8.1.60.0060.00317.72
8.1.50.0000.00817.54
8.1.40.0000.00817.52
8.1.30.0040.00417.66
8.1.20.0030.00517.59
8.1.10.0000.00817.71
8.1.00.0080.00417.65
8.0.300.0040.00419.09
8.0.290.0020.00517.30
8.0.280.0000.00818.56
8.0.270.0000.00717.30
8.0.260.0000.00717.25
8.0.250.0030.00317.08
8.0.240.0040.00417.11
8.0.230.0000.00817.03
8.0.220.0040.00417.03
8.0.210.0000.00716.95
8.0.200.0040.00417.06
8.0.190.0060.00317.05
8.0.180.0120.00017.03
8.0.170.0050.00316.88
8.0.160.0000.00716.90
8.0.150.0000.00716.92
8.0.140.0030.00516.96
8.0.130.0060.00013.39
8.0.120.0040.00416.96
8.0.110.0040.00416.91
8.0.100.0060.00316.84
8.0.90.0000.00717.02
8.0.80.0160.00017.01
8.0.70.0000.00816.82
8.0.60.0040.00416.83
8.0.50.0000.00717.03
8.0.30.0140.00717.21
8.0.20.0090.01017.40
8.0.10.0040.00417.11
8.0.00.0110.00716.88
7.4.330.0060.00015.03
7.4.320.0060.00016.71
7.4.300.0000.00716.73
7.4.290.0040.00416.76
7.4.280.0000.00716.61
7.4.270.0070.00016.72
7.4.260.0040.00416.68
7.4.250.0000.00816.72
7.4.240.0020.00616.59
7.4.230.0000.00716.72
7.4.220.0110.00716.73
7.4.210.0070.00716.80
7.4.200.0040.00416.55
7.4.160.0080.01116.68
7.4.150.0150.00317.40
7.4.140.0110.01117.86
7.4.130.0090.00916.61
7.4.120.0070.01216.70
7.4.110.0070.01416.46
7.4.100.0100.01016.43
7.4.90.0140.00316.50
7.4.80.0070.01019.39
7.4.70.0060.01216.55
7.4.60.0100.01316.62
7.4.50.0060.00016.59
7.4.40.0100.00716.72
7.4.30.0140.01416.76
7.4.00.0110.00714.93
7.3.330.0000.00513.34
7.3.320.0040.00413.43
7.3.310.0030.00316.43
7.3.300.0000.00716.35
7.3.290.0050.01016.38
7.3.280.0110.00816.43
7.3.270.0150.00317.40
7.3.260.0090.00916.79
7.3.240.0090.00616.54
7.3.230.0070.01016.48
7.3.210.0100.00616.78
7.3.200.0090.00919.39
7.3.190.0030.01316.53
7.3.180.0100.00716.53
7.3.170.0070.01016.65
7.3.160.0030.01416.67
7.3.10.0040.01216.70
7.3.00.0030.00616.89
7.2.330.0070.01016.88
7.2.320.0100.01016.70
7.2.310.0110.00716.83
7.2.300.0120.00616.88
7.2.290.0090.00816.94
7.2.130.0060.00616.96
7.2.120.0000.01316.82
7.2.110.0000.01516.79
7.2.100.0060.00917.03
7.2.90.0030.00717.04
7.2.80.0030.01017.05
7.2.70.0030.00917.00
7.2.60.0060.00816.97
7.2.50.0090.00516.98
7.2.40.0070.00417.18
7.2.30.0100.00316.69
7.2.20.0090.00617.06
7.2.10.0060.00317.05
7.2.00.0030.01317.09
7.1.250.0080.00415.63
7.1.200.0120.00015.55
7.1.70.0000.01116.76
7.1.60.0120.01219.57
7.1.50.0070.01717.07
7.1.00.0030.07722.36
7.0.200.0050.00316.83
7.0.140.0070.07022.16
7.0.70.0100.07321.70
7.0.60.0130.05021.71
7.0.50.0070.07722.09
7.0.40.0100.08020.15
7.0.30.0100.04320.11
7.0.20.0070.08720.14
7.0.10.0030.09020.18
7.0.00.0030.08720.11
5.6.280.0070.07321.12
5.6.220.0030.08720.67
5.6.210.0100.07720.69
5.6.200.0000.09321.15
5.6.190.0100.08021.08
5.6.180.0070.05321.07
5.6.170.0070.04721.18
5.6.160.0030.06321.18
5.6.150.0070.08321.18
5.6.140.0030.04021.07
5.6.130.0070.08321.08
5.6.120.0030.08720.98
5.6.110.0000.08721.17
5.6.100.0100.08021.00
5.6.90.0070.07320.98
5.6.80.0070.06720.51
5.6.70.0000.07720.45
5.6.60.0030.07020.37
5.6.50.0070.08020.57
5.6.40.0030.07020.35
5.6.30.0070.07320.34
5.6.20.0070.05320.35
5.6.10.0100.04320.51
5.6.00.0070.07720.50
5.5.360.0030.06020.40
5.5.350.0000.06020.45
5.5.340.0130.07320.84
5.5.330.0070.08320.93
5.5.320.0000.04720.98
5.5.310.0030.09020.93
5.5.300.0030.04020.97
5.5.290.0030.08320.84
5.5.280.0070.07020.94
5.5.270.0070.08720.84
5.5.260.0130.07720.90
5.5.250.0070.07720.72
5.5.240.0130.06320.35
5.5.230.0030.08720.34
5.5.220.0030.08720.34
5.5.210.0070.04320.26
5.5.200.0030.08320.25
5.5.190.0070.07720.16
5.5.180.0100.07720.20
5.5.160.0070.08320.29
5.5.150.0000.07720.32
5.5.140.0130.06720.04
5.5.130.0130.07720.25
5.5.120.0070.07720.20
5.5.110.0130.05720.29
5.5.100.0000.08720.16
5.5.90.0030.05320.12
5.5.80.0070.07720.21
5.5.70.0100.07320.11
5.5.60.0000.08320.20
5.5.50.0070.04320.20
5.5.40.0030.07720.14
5.5.30.0030.04020.15
5.5.20.0030.04019.98
5.5.10.0070.07020.15
5.5.00.0000.04320.16
5.4.450.0000.08319.42
5.4.440.0070.08019.56
5.4.430.0070.07719.26
5.4.420.0130.07319.32
5.4.410.0100.05019.32
5.4.400.0070.06319.19
5.4.390.0070.05719.05
5.4.380.0030.07718.95
5.4.370.0100.07718.95
5.4.360.0030.08019.12
5.4.350.0030.04719.14
5.4.340.0070.08019.05
5.4.320.0000.07318.95
5.4.310.0070.07319.09
5.4.300.0170.06319.16
5.4.290.0000.05719.04
5.4.280.0030.06318.89
5.4.270.0000.08019.13
5.4.260.0070.07019.13
5.4.250.0000.08019.22
5.4.240.0000.04318.89
5.4.230.0000.04718.84
5.4.220.0000.05718.86
5.4.210.0030.07719.11
5.4.200.0000.07018.84
5.4.190.0030.04019.08
5.4.180.0130.07319.02
5.4.170.0070.05019.12
5.4.160.0100.03319.13
5.4.150.0000.04318.87
5.4.140.0070.03316.39
5.4.130.0030.05316.50
5.4.120.0070.04716.48
5.4.110.0000.04016.44
5.4.100.0000.04016.55
5.4.90.0100.03016.40
5.4.80.0000.03716.51
5.4.70.0070.04016.38
5.4.60.0000.03716.41
5.4.50.0030.03316.52
5.4.40.0000.03716.52
5.4.30.0070.06016.36
5.4.20.0000.04016.37
5.4.10.0030.03716.32
5.4.00.0000.03715.96

preferences:
18.93 ms | 401 KiB | 5 Q