3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Person { public readonly string $lastName; public readonly string $firstName; private readonly int $strength; private readonly int $agility; private readonly int $intelligence; private readonly int $charisma; private readonly int $luck; /** * @param non-empty-string|null $lastName */ public function __construct(?string $lastName = null) { $this->lastName = $lastName ?? $this->randomName(); $this->firstName = $firstName ?? $this->randomName($this->lastName[0]); $this->strength = $this->randomFeatureValue(); $this->agility = $this->randomFeatureValue(); $this->intelligence = $this->randomFeatureValue(); $this->charisma = $this->randomFeatureValue(); $this->luck = $this->randomFeatureValue(); } public function __clone(): void { $this->firstName = $this->randomName($this->lastName[0]); $this->strength = $this->evolveFeatureValue($this->strength); $this->agility = $this->evolveFeatureValue($this->agility); $this->intelligence = $this->evolveFeatureValue($this->intelligence); $this->charisma = $this->evolveFeatureValue($this->charisma); $this->luck = $this->evolveFeatureValue($this->luck); } public function getName(): string { return $this->lastName . ' ' . $this->firstName; } public function getValue(): int { return $this->strength + $this->agility + $this->intelligence + $this->charisma + $this->luck; } public function getDetails(): string { return $this->getName() . ' ' . $this->getValue() . '(' . $this->strength . '+' . $this->agility . '+' . $this->intelligence . '+' . $this->charisma . '+' . $this->luck . ')'; } /** * @return int * @throws Exception */ private function randomFeatureValue(): int { return random_int(0, 9); } private function evolveFeatureValue(int $value): int { return min(max($value + random_int(-1, 1), 0), 9); } public function randomName(?string $notStartingWith = null): string { $possibleNames = ['Alice', 'Bob', 'Charlie', 'Dave', 'Eve', 'Frank', 'Grace', 'Heidi', 'Ivan', 'Judy', 'Kevin', 'Linda', 'Mallory', 'Niaj', 'Olivia', 'Peggy', 'Quentin', 'Rupert', 'Sybil', 'Trent', 'Ursula', 'Victor', 'Walter', 'Xavier', 'Yvonne', 'Zoe']; do { $randomName = $possibleNames[array_rand($possibleNames)]; } while ($notStartingWith !== null && $randomName[0] === $notStartingWith); return $randomName; } } class Life { /** @var Person[] */ private array $population = []; public function run(int $iterations) { while ($iterations-- > 0) { $this->runIteration(); } } public function runIteration() { $this->new(); $this->reproduce(); } public function geStatisticsOnMostValuable() { $topMostValuablePersons = $this->getTopMostValuablePersons(10); $statistics = array_map( fn(Person $person) => $person->getDetails(), $topMostValuablePersons, ); return implode("\n", $statistics); } private function new(): void { foreach (range(1, 3) as $_) { $this->addPerson(new Person()); } } private function reproduce(): void { $topMostValuablePerson = $this->getTopMostValuablePersons(5); // usual evolution foreach (range(1, 3) as $_) { $randomValuablePerson = $topMostValuablePerson[array_rand($topMostValuablePerson)]; $this->addPerson(clone $randomValuablePerson); } // tracked evolution $randomValuablePerson = $topMostValuablePerson[array_rand($topMostValuablePerson)]; $this->addPerson(clone $randomValuablePerson with (lastName: '0000')); } private function addPerson(Person $person) { /** add a person, eventually killing one that already has the same name */ $this->population[$person->getName()] = $person; } /** * @param int $int * @return Person[] */ private function getTopMostValuablePersons(int $int): array { $population = $this->population; uasort($population, function (Person $person1, Person $person2) { return $person2->getValue() <=> $person1->getValue(); }); return array_slice($population, 0, $int); } } $life = new Life(); $life->run(100); echo $life->geStatisticsOnMostValuable();

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.2.60.0030.00617.50
8.2.50.0180.00017.50
8.2.40.0170.00017.25
8.2.30.0110.00517.38
8.2.20.0170.00017.21
8.2.10.0140.00317.35
8.2.00.0170.00017.00
8.1.190.0100.00017.00
8.1.180.0080.00816.86
8.1.170.0150.00216.88
8.1.160.0100.00817.00
8.1.150.0060.01216.99
8.1.140.0130.00617.13
8.1.130.0170.00017.13
8.1.120.0100.00717.34
8.1.110.0080.00817.13
8.1.100.0130.00317.00
8.1.90.0110.00516.98
8.1.80.0130.00416.98
8.1.70.0160.00217.22
8.1.60.0170.00317.13
8.1.50.0090.00916.98
8.1.40.0060.01217.00
8.1.30.0170.00017.13
8.1.20.0130.00417.25
8.1.10.0090.00917.25
8.1.00.0220.00017.13
8.0.280.0120.00416.52
8.0.270.0100.00616.52
8.0.260.0130.00316.75
8.0.250.0150.00016.52
8.0.240.0070.00716.52
8.0.230.0140.00216.63
8.0.220.0100.00516.52
8.0.210.0120.00416.63
8.0.200.0160.00016.75
8.0.190.0110.00616.61
8.0.180.0130.00216.63
8.0.170.0140.00316.59
8.0.160.0110.00816.52
8.0.150.0110.00616.63
8.0.140.0140.00316.52
8.0.130.0160.00016.52
8.0.120.0060.00916.75
8.0.110.0120.00316.52
8.0.100.0080.00816.63
8.0.90.0100.00516.52
8.0.80.0160.00016.52
8.0.70.0130.00416.52
8.0.60.0110.00516.80
8.0.50.0080.00816.52
8.0.30.0100.00616.63
8.0.20.0110.00416.52
8.0.10.0150.00016.52

preferences:
138.01 ms | 993 KiB | 7 Q