3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Time { private static array $map = []; public float $seconds { get => $this->milliseconds / 1000.0; } public float $minutes { get => $this->seconds / 60.0; } private static function getValue(int $milliseconds): Time { // Attempt to get the value from the array, and if it exists, get the // value from the WeakReference, otherwise, create a new one $realValue = (self::$map[$milliseconds] ?? null)?->get() ?? new self($milliseconds); // Store the value in the array, even if another reference exists self::$map[$milliseconds] = WeakReference::create($realValue); return $realValue; } public function __destruct(){ // The values no longer exist, and we can delete the value from the array unset(self::$map[$this->milliseconds]); } private function __construct(public readonly int $milliseconds) {} public static function fromMilliseconds(int $milliseconds): Time { return self::getValue($milliseconds); } public static function fromSeconds(float $seconds): Time { return self::getValue($seconds * 1000); } public static function fromMinutes(float $minutes): Time { return self::getValue($minutes * 60000); } } var_dump(Time::fromSeconds(10) === Time::fromMilliseconds(10000));

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.90.0220.01126.77

preferences:
136.09 ms | 1421 KiB | 7 Q