3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface Equalable { function equal(User $otherUser): bool; } class Transaction { function __construct( public int $amount, public string $type, ) {} } class User implements Equalable { private array $transactions = []; function __construct(public string $name) {} // Assume this is operator ==(...) function equal(User $otherUser): bool { // Junior developer only has use-case where he needs to the username. return $otherUser->name === $this->name; } function mergeTransactions(User $user) { $this->transactions = array_merge($this->transactions, $user->transactions); } function addTransaction(Transaction $transaction) { $this->transactions[] = $transaction; } function getTransactions(): array { return $this->transactions; } } function mergeUsers() { $users = []; foreach( ObjectCache::getCache() as $user) { if (!array_key_exists($user->name, $users)) { $users[$user->name] = $user; } if ($users[$user->name] !== $user) { $users[$user->name]->mergeTransactions($user); } } ObjectCache::set($users); } class ObjectCache { private static string $method; private static array $objects; static function getCache(): array { return self::$objects; } static function add(Equalable $eq): void { if (count(self::$objects) === 0) { self::$objects[] = $eq; return; } foreach (self::$objects as $object) { switch (self::$method) { case 'built-in': if ($eq != $object) { self::$objects[] = $eq; break; } break; case 'op-overload': // Assume this is $eq != $object if (!$eq->equal($object)) { self::$objects[] = $object; break; } break; } } } static function set(array $objects): void { self::$objects = $objects; } static function init(string $method): void { self::$method = $method; self::$objects = []; } static function search(string $prop, mixed $value): ?object { $values = array_column(self::$objects,$prop); $index = array_search($value,$values); return $index !== false ? self::$objects[$index] : null; } } function loadFromDB() { $u = new User( "John" ); $u->addTransaction(new Transaction(100,'credit')); $u->addTransaction(new Transaction(500,'credit')); $u->addTransaction(new Transaction(250,'credit')); return $u; } foreach( ['built-in','op-overload'] as $method) { ObjectCache::init($method); echo "Method: {$method}\n"; $john = new User( "John" ); ObjectCache::add( $john ); ObjectCache::add( loadFromDB()); mergeUsers(); print_r(ObjectCache::getCache()); }

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.4.120.0090.00924.14
8.4.110.0120.00818.94
8.4.100.0100.01018.76
8.4.90.0100.00720.80
8.4.80.0110.01018.88
8.4.70.0020.00718.02
8.4.60.0090.00918.40
8.4.50.0130.00919.50
8.4.40.0100.01017.59
8.4.30.0110.00823.98
8.4.20.0120.00619.41
8.4.10.0090.00019.61
8.3.250.0090.01017.18
8.3.240.0120.00717.38
8.3.230.0130.00716.42
8.3.220.0070.00718.91
8.3.210.0120.00716.64
8.3.200.0050.00416.51
8.3.190.0050.00418.98
8.3.180.0100.00818.36
8.3.170.0110.00717.00
8.3.160.0110.00720.60
8.3.150.0030.01617.48
8.3.140.0060.00317.23
8.3.130.0080.00018.59
8.3.120.0040.01519.32
8.3.110.0150.01017.41
8.3.100.0130.00318.43
8.3.50.0150.00018.38
8.2.290.0060.00222.14
8.2.280.0100.01018.45
8.2.270.0040.01616.86
8.2.260.0040.00417.21
8.2.250.0040.00417.22
8.2.240.0070.01418.22
8.2.230.0120.00318.22
8.2.220.0060.01218.24
8.1.330.0140.00622.07
8.1.320.0120.00916.03
8.1.310.0060.00316.72
8.1.300.0100.00018.10

preferences:
27.06 ms | 403 KiB | 5 Q