@ 2024-09-17T22:26:10Z <?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());
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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).
Version System time (s) User time (s) Memory (MiB) 8.4.12 0.009 0.009 24.14 8.4.11 0.012 0.008 18.94 8.4.10 0.010 0.010 18.76 8.4.9 0.010 0.007 20.80 8.4.8 0.011 0.010 18.88 8.4.7 0.002 0.007 18.02 8.4.6 0.009 0.009 18.40 8.4.5 0.013 0.009 19.50 8.4.4 0.010 0.010 17.59 8.4.3 0.011 0.008 23.98 8.4.2 0.012 0.006 19.41 8.4.1 0.009 0.000 19.61 8.3.25 0.009 0.010 17.18 8.3.24 0.012 0.007 17.38 8.3.23 0.013 0.007 16.42 8.3.22 0.007 0.007 18.91 8.3.21 0.012 0.007 16.64 8.3.20 0.005 0.004 16.51 8.3.19 0.005 0.004 18.98 8.3.18 0.010 0.008 18.36 8.3.17 0.011 0.007 17.00 8.3.16 0.011 0.007 20.60 8.3.15 0.003 0.016 17.48 8.3.14 0.006 0.003 17.23 8.3.13 0.008 0.000 18.59 8.3.12 0.004 0.015 19.32 8.3.11 0.015 0.010 17.41 8.3.10 0.013 0.003 18.43 8.3.5 0.015 0.000 18.38 8.2.29 0.006 0.002 22.14 8.2.28 0.010 0.010 18.45 8.2.27 0.004 0.016 16.86 8.2.26 0.004 0.004 17.21 8.2.25 0.004 0.004 17.22 8.2.24 0.007 0.014 18.22 8.2.23 0.012 0.003 18.22 8.2.22 0.006 0.012 18.24 8.1.33 0.014 0.006 22.07 8.1.32 0.012 0.009 16.03 8.1.31 0.006 0.003 16.72 8.1.30 0.010 0.000 18.10
preferences:dark mode live preview ace vim emacs key bindings
27.06 ms | 403 KiB | 5 Q