3v4l.org

run code in 300+ PHP versions simultaneously
<?php #BlackLivesMatter - github.com/ghostwriter namespace PolynomialFunctors; use InvalidArgumentException; /** * Represents a polynomial functor of the form `1 + x + x^2`. * * @template T * @implements Functor<T> */ final class Polynomial implements Functor { private int $tag; private mixed $value; private function __construct(int $tag, mixed $value = null) { $this->tag = $tag; $this->value = $value; } /** * Represents the `1` case. * * @return self */ public static function unit(): self { return new self(0); } /** * Represents the `x` case with one value. * * @template T * @param T $value * @return self<T> */ public static function linear(mixed $value): self { return new self(1, $value); } /** * Represents the `x^2` case with two values. * * @template T * @param T $first * @param T $second * @return self<T> */ public static function quadratic(mixed $first, mixed $second): self { return new self(2, [$first, $second]); } /** * Maps a function over the polynomial structure. * * @template A * @template B * @param callable(A): B $f * @return self<B> */ public function map(callable $f): Polynomial { return match ($this->tag) { 0 => self::unit(), 1 => self::linear($f($this->value)), 2 => self::quadratic($f($this->value[0]), $f($this->value[1])), default => throw new InvalidArgumentException('Unknown polynomial tag') }; } } interface Functor { /** * Maps a function over the functor. * * @template A * @template B * @param callable(A): B $f * @return Functor<B> */ public function map(callable $f): Functor; } $unit = Polynomial::unit(); // Represents `1` $linear = Polynomial::linear(5); // Represents `x` $quadratic = Polynomial::quadratic(3, 7); // Represents `x^2` // Mapping over the functor $mappedLinear = $linear->map(fn(int $x): int => $x * 2); // Maps over the single value $mappedQuadratic = $quadratic->map(fn(int $x): int => $x + 1); // Maps over both values // Output results var_dump($unit); // Nothing to map var_dump($mappedLinear); // Polynomial with value 10 var_dump($mappedQuadratic); // Polynomial with values 4 and 8

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.5.00.0110.01120.04
8.4.150.0030.00014.05
8.4.140.0150.00817.80
8.4.130.0070.00717.93
8.4.120.0130.00622.25
8.4.110.0100.01018.73
8.4.100.0120.00817.71
8.4.90.0090.01120.58
8.4.80.0060.00219.07
8.4.70.0100.00618.71
8.4.60.0160.00720.49
8.4.50.0110.01018.75
8.4.40.0100.01017.75
8.4.30.0130.00723.80
8.4.20.0140.00719.36
8.4.10.0040.00419.19
8.3.280.0110.01018.47
8.3.270.0150.00516.50
8.3.260.0090.00816.85
8.3.250.0120.00721.12
8.3.240.0150.00416.43
8.3.230.0120.00816.40
8.3.220.0070.00518.97
8.3.210.0050.00316.48
8.3.200.0050.00516.63
8.3.190.0100.00918.96
8.3.180.0130.00718.89
8.3.170.0090.00917.33
8.3.160.0090.00620.81
8.3.150.0100.01016.96
8.3.140.0050.00318.45
8.3.130.0000.00816.50
8.3.120.0150.00418.44
8.3.110.0120.00718.29
8.3.100.0220.00018.41
8.3.90.0180.00416.32
8.3.80.0170.00616.23
8.3.70.0200.00416.66
8.3.60.0210.00318.33
8.3.50.0150.00716.53
8.3.40.0130.00917.52
8.3.30.0170.00217.53
8.3.20.0110.00917.42
8.3.10.0100.01017.21
8.3.00.0110.00419.26
8.2.290.0070.00417.57
8.2.280.0130.00618.29
8.2.270.0130.00616.70
8.2.260.0030.01317.17
8.2.250.0100.01016.83
8.2.240.0120.00818.27
8.2.230.0140.00516.34
8.2.220.0120.00616.61
8.2.210.0160.00516.34
8.2.200.0160.00316.67
8.2.190.0130.00616.63
8.2.180.0190.00016.21
8.2.170.0160.00417.30
8.2.160.0150.00517.33
8.2.150.0150.00517.33
8.2.140.0200.00017.51
8.2.130.0170.00717.08
8.2.120.0180.00517.34
8.2.110.0170.00317.45
8.2.100.0170.00317.34
8.2.90.0150.00417.26
8.2.80.0170.00217.65
8.2.70.0170.00317.52
8.2.60.0190.00017.48
8.2.50.0150.00417.22
8.2.40.0190.00017.52
8.2.30.0160.00317.56
8.2.20.0160.00217.41
8.2.10.0120.00617.32
8.2.00.0130.00717.41
8.1.330.0100.01016.49
8.1.320.0120.00817.86
8.1.310.0100.00718.50
8.1.300.0030.00620.00
8.1.290.0160.00315.80
8.1.280.0200.00015.75
8.1.270.0190.00317.21
8.1.260.0170.00418.94
8.1.250.0090.01217.52
8.1.240.0080.00817.33
8.1.230.0140.00617.21
8.1.220.0170.00217.29
8.1.210.0150.00417.21
8.1.200.0140.00517.10
8.1.190.0140.00516.93
8.1.180.0180.00017.04
8.1.170.0140.00516.61
8.1.160.0090.00917.07
8.1.150.0140.00317.29
8.1.140.0130.00616.90
8.1.130.0160.00417.13
8.1.120.0150.00319.00
8.1.110.0130.00317.11
8.1.100.0150.00318.89
8.1.90.0080.01117.21
8.1.80.0090.00917.00
8.1.70.0140.00517.11
8.1.60.0160.00317.32
8.1.50.0160.00417.02
8.1.40.0170.00517.25
8.1.30.0190.00417.12
8.1.20.0160.00417.48
8.1.10.0150.00517.22
8.1.00.0130.00816.92

preferences:
31.66 ms | 403 KiB | 5 Q