3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Parent class for custom model classes. * * This class was implemented like part of Observer pattern * https://en.wikipedia.org/wiki/Observer_pattern * http://php.net/manual/en/class.splsubject.php */ class Model implements SplSubject { /** * @var object List of attached observerer */ private $observers; /** * @var array Data for notify to observerer */ private $updates = []; /** * Class Constructor. */ public function __construct() { $this->observers = new SplObjectStorage(); } /** * Attach an Observer class to this Subject for updates * when occour a subject state change. * * @param SplObserver $observer */ public function attach(SplObserver $observer) { if ($observer instanceof View) { $this->observers->attach($observer); } } /** * Detach an Observer class from this Subject. * * @param SplObserver $observer */ public function detach(SplObserver $observer) { if ($observer instanceof View) { $this->observers->detach($observer); } } /** * Notify a state change of Subject to all registered Observeres. */ public function notify() { foreach ($this->observers as $value) { $value->update($this); } } /** * Set the data to notify to all registered Observeres. * * @param array $data */ public function set(array $data) { $this->updates = array_merge_recursive($this->updates, $data); } /** * Get the data to notify to all registered Observeres. * * @return array */ public function get(): array { return $this->updates; } } /** * Parent class for custom view classes. * * This class was implemented like part of Observer pattern * https://en.wikipedia.org/wiki/Observer_pattern * http://php.net/manual/en/class.splobserver.php */ class View implements SplObserver { /** * @var array Data for the dynamic view */ protected $data = []; /** * @var string Output data */ protected $output = ''; /** * @var Model Model for access data */ protected $model; /** * Class Constructor. * * @param Model $model */ public function __construct(Model $model) { $this->model = $model; } /** * Render a template. */ public function render(): string { return $this->output; } /** * Update Observer data. * * @param SplSubject $subject */ public function update(SplSubject $subject) { if ($subject instanceof Model) { $this->data = array_merge($this->data, $subject->get()); } } } /** * Parent class for custom controller classes. */ class Controller { /** * @var object The model object for current controller */ protected $model = null; /** * Class Constructor. * * @param object $model */ public function __construct(Model $model) { $this->model = $model; } } /** * Our Custom Model */ class CalculatorModel extends Model { /** * Class Constructor. */ public function __construct() { parent::__construct(); } /** * Mutiply business logic. * * @param array $numbers */ public function multiply(array $numbers) { $this->set([ 'result' => $this->operation('*', $numbers), 'operands' => $numbers, ]); } /** * Divide business logic. * * @param array $numbers */ public function divide(array $numbers) { $this->set([ 'result' => $this->operation('/', $numbers), 'operands' => $numbers ]); } /** * Subtraction business logic. * * @param array $numbers */ public function sub(array $numbers) { $this->set([ 'result' => $this->operation('-', $numbers), 'operands' => $numbers ]); } /** * Addition business logic. * * @param array $numbers */ public function add(array $numbers) { $this->set([ 'result' => $this->operation('+', $numbers), 'operands' => $numbers ]); } /** * Do math operation. * This is an example, division by 0 and other problematic things not checked. * * @param string $operator * @param array $numbers * @return int|float */ private function operation(string $operator, array $numbers) { $temp = null; foreach ($numbers as $n) { if ($temp === null) { $temp = $n; continue; } //example, division by 0 and other not checked switch ($operator) { case '*': $temp = $temp * $n; break; case '/': $temp = $temp / $n; break; case '-': $temp = $temp - $n; break; case '+': $temp = $temp + $n; break; } } return $temp; } } /** * Our Custom Model */ class CalculatorView extends View { /** * Class Constructor. * * @param CalculatorModel $model */ public function __construct(CalculatorModel $model) { parent::__construct($model); } /** * Build output for multiply. */ public function multiply() { $this->output = $this->generateOutput( 'Multiplication', $this->data['operands'], $this->data['result'] ); } /** * Build output for divide. */ public function divide() { $this->output = $this->generateOutput( 'Division', $this->data['operands'], $this->data['result'] ); } /** * Build output for add. */ public function add() { $this->output = $this->generateOutput( 'Addiction', $this->data['operands'], $this->data['result'] ); } /** * Build output for sub. */ public function sub() { $this->output = $this->generateOutput( 'Subtraction', $this->data['operands'], $this->data['result'] ); } /** * Generate the output. * * @param string $operation * @param array $operand * @param type $result * * @return string */ private function generateOutput(string $operation, array $operands, $result): string { $string = $operation . ': '; foreach ($operands as $value) { $string .= $value . ' * '; } $string = substr($string, 0, strlen($string) - 2); $string .= '= ' . $result; return $string; } } /** * Our Custom Controller */ class CalculatorController extends Controller { /** * Class Constructor. * * @param CalculatorModel $model */ public function __construct(CalculatorModel $model) { parent::__construct($model); } /** * Multiply input point. * * @param mixed $numbers */ public function multiply(... $numbers) { //check user input $this->checkOperands($numbers); $this->filter($numbers); //manipulate the model $this->model->multiply($numbers); } /** * Divide input point. * * @param mixed $numbers */ public function divide(... $numbers) { //check user input $this->checkOperands($numbers); $this->filter($numbers); //manipulate the model $this->model->divide($numbers); } /** * Sub input point. * * @param mixed $numbers */ public function sub(... $numbers) { //check user input $this->checkOperands($numbers); $this->filter($numbers); //manipulate the model $this->model->sub($numbers); } /** * Add input point. * * @param mixed $numbers */ public function add(... $numbers) { //check user input $this->checkOperands($numbers); $this->filter($numbers); //manipulate the model $this->model->add($numbers); } /** * Filter user supplied numbers. * * @param mixed $numbers * @throws InvalidArgumentException */ private function filter(&$numbers) { foreach ($numbers as $key => $number) { switch (gettype($number)) { case 'string': $number[$key] = strtonum($number); break; case 'integer': break; case 'double': break; default: throw new InvalidArgumentException('Not a number'); } } } /** * Convert a number given as string in the proper type (int or float). * https://secure.php.net/manual/en/language.types.type-juggling.php * * @param string $string Number as string ex '1.0', '0.9' etc * @return int|float */ private function strtonum(string $string) { if (fmod((float) $string, 1.0) === 0.0) { return (int) $string; } return (float) $string; } /** * Check minimum numbers required for operations. * * @param array $numbers * @throws ArgumentCountError */ private function checkOperands(array &$numbers) { if (count($numbers) < 2) { throw new ArgumentCountError('Two number needed for operation'); } } } //create components $model = new CalculatorModel(); $view = new CalculatorView($model); $controller = new CalculatorController($model); //attach the observer $model->attach($view); //call controller $controller->multiply(2, 3); //notify changes to observer $model->notify(); //call view, build output for selected operation $view->multiply(); //get the output echo $view->render();

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.130.0060.00318.70
8.3.120.0130.00618.85
8.3.110.0090.00020.94
8.3.100.0060.00316.83
8.3.90.0050.00326.77
8.3.80.0030.00617.00
8.3.70.0070.00718.42
8.3.60.0120.00916.86
8.3.50.0130.00516.74
8.3.40.0070.01118.96
8.3.30.0110.01119.09
8.3.20.0040.00420.38
8.3.10.0120.00323.39
8.3.00.0040.00423.66
8.2.250.0060.00318.54
8.2.240.0000.00917.20
8.2.230.0030.00622.58
8.2.220.0060.00324.06
8.2.210.0070.00326.77
8.2.200.0100.00016.88
8.2.190.0080.00816.88
8.2.180.0160.00618.41
8.2.170.0090.00622.96
8.2.160.0160.00322.13
8.2.150.0040.00425.66
8.2.140.0110.00724.66
8.2.130.0070.01126.16
8.2.120.0040.00420.99
8.2.110.0100.00021.04
8.2.100.0030.01017.84
8.2.90.0040.00422.34
8.2.80.0040.00418.05
8.2.70.0000.00817.93
8.2.60.0090.00017.93
8.2.50.0060.00318.34
8.2.40.0000.00820.59
8.2.30.0070.00419.46
8.2.20.0080.00018.30
8.2.10.0040.00418.15
8.2.00.0000.00718.21
8.1.300.0060.01319.88
8.1.290.0060.00330.84
8.1.280.0040.01125.92
8.1.270.0080.00023.91
8.1.260.0000.00826.35
8.1.250.0060.00928.09
8.1.240.0070.00421.06
8.1.230.0110.00021.20
8.1.220.0040.00417.79
8.1.210.0040.00419.01
8.1.200.0000.00817.60
8.1.190.0030.00617.36
8.1.180.0040.00418.10
8.1.170.0000.00819.03
8.1.160.0040.00418.97
8.1.150.0040.00418.87
8.1.140.0040.00417.69
8.1.130.0030.00618.89
8.1.120.0030.00617.51
8.1.110.0030.00517.45
8.1.100.0040.00417.55
8.1.90.0040.00417.54
8.1.80.0000.00817.46
8.1.70.0000.00717.38
8.1.60.0070.00117.57
8.1.50.0000.00917.45
8.1.40.0030.00617.51
8.1.30.0060.00317.70
8.1.20.0030.00517.70
8.1.10.0030.00617.51
8.1.00.0000.00817.57
8.0.300.0060.00320.04
8.0.290.0030.00616.88
8.0.280.0050.00218.42
8.0.270.0020.00517.31
8.0.260.0030.00316.96
8.0.250.0000.00717.16
8.0.240.0030.00317.13
8.0.230.0000.00716.98
8.0.220.0040.00417.04
8.0.210.0040.00416.87
8.0.200.0050.00217.12
8.0.190.0030.00516.96
8.0.180.0000.00816.94
8.0.170.0020.00517.03
8.0.160.0050.00216.93
8.0.150.0000.00716.99
8.0.140.0040.00416.99
8.0.130.0000.00613.44
8.0.120.0020.00516.95
8.0.110.0040.00417.07
8.0.100.0060.00317.07
8.0.90.0030.00517.05
8.0.80.0040.01517.00
8.0.70.0000.00717.05
8.0.60.0020.00517.07
8.0.50.0000.00816.87
8.0.30.0120.00617.06
8.0.20.0120.01117.40
8.0.10.0040.00417.00
8.0.00.0120.00616.89
7.4.330.0030.00316.64
7.4.320.0030.00316.56
7.4.300.0060.00016.60
7.4.290.0050.00216.66
7.4.280.0060.00316.57
7.4.270.0000.00816.68
7.4.260.0080.00016.64
7.4.250.0000.00716.50
7.4.240.0030.00316.59
7.4.230.0080.00016.66
7.4.220.0160.01016.59
7.4.210.0080.01016.68
7.4.200.0030.00516.64
7.4.160.0060.01216.59
7.4.150.0030.01517.40
7.4.140.0080.00917.86
7.4.130.0120.00916.52
7.4.120.0100.00916.60
7.4.110.0090.00916.59
7.4.100.0170.00016.66
7.4.90.0150.00916.70
7.4.80.0000.01719.39
7.4.70.0100.01416.71
7.4.60.0100.00716.34
7.4.50.0050.00516.35
7.4.40.0110.00716.61
7.4.30.0110.00516.64
7.4.10.0150.00314.81
7.4.00.0090.00714.91
7.3.330.0050.00213.54
7.3.320.0000.00513.48
7.3.310.0000.00816.53
7.3.300.0070.00016.39
7.3.290.0050.00316.34
7.3.280.0090.00816.45
7.3.270.0070.01017.40
7.3.260.0120.00616.59
7.3.250.0130.00816.48
7.3.240.0090.01016.49
7.3.230.0120.00616.70
7.3.210.0170.00316.59
7.3.200.0100.00616.38
7.3.190.0100.00716.65
7.3.180.0100.00716.62
7.3.170.0060.00916.44
7.3.160.0100.00716.64
7.3.130.0090.00914.98
7.3.120.0100.01014.87
7.3.110.0040.01514.65
7.3.100.0030.01015.00
7.3.90.0100.00715.04
7.3.80.0060.00615.08
7.3.70.0100.00314.96
7.3.60.0060.01214.80
7.3.50.0060.00914.98
7.3.40.0110.00315.04
7.3.30.0040.01215.08
7.3.20.0070.01116.62
7.3.10.0080.00616.75
7.3.00.0070.00316.89
7.2.330.0070.01016.91
7.2.320.0070.01016.93
7.2.310.0150.00816.84
7.2.300.0090.01216.83
7.2.290.0000.01816.77
7.2.260.0070.01415.28
7.2.250.0040.01515.32
7.2.240.0070.00715.42
7.2.230.0070.01015.26
7.2.220.0070.00715.10
7.2.210.0040.01514.99
7.2.200.0040.00715.13
7.2.190.0000.01014.98
7.2.180.0050.00515.36
7.2.170.0060.01215.25
7.2.160.0000.01615.30
7.2.150.0100.00317.14
7.2.140.0030.00716.72
7.2.130.0040.01216.96
7.2.120.0110.00616.79
7.2.110.0030.00916.97
7.2.100.0060.00317.04
7.2.90.0060.01017.15
7.2.80.0070.01017.19
7.2.70.0120.00417.07
7.2.60.0090.00716.82
7.2.50.0080.00717.04
7.2.40.0000.01717.05
7.2.30.0130.00717.06
7.2.20.0060.01316.93
7.2.10.0030.00716.97
7.2.00.0060.00917.01
7.1.330.0000.01315.88
7.1.320.0060.00615.84
7.1.310.0070.00715.64
7.1.300.0120.00615.99
7.1.290.0070.01015.74
7.1.280.0030.01315.60
7.1.270.0060.01315.88
7.1.260.0000.01415.89
7.1.250.0060.00315.69
7.1.240.0050.00515.91
7.1.230.0120.00316.00
7.1.220.0030.01315.79
7.1.210.0040.00715.91
7.1.200.0050.00915.88
7.1.190.0120.00315.80
7.1.180.0030.01515.55
7.1.170.0060.01215.61
7.1.160.0060.00915.85
7.1.150.0100.01015.77
7.1.140.0040.01215.58
7.1.130.0000.00915.98
7.1.120.0110.00315.89
7.1.110.0080.00917.27
7.1.100.0090.01017.10
7.1.90.0090.00917.31
7.1.80.0080.01117.14
7.1.70.0130.00616.33
7.1.60.0250.00929.94
7.1.50.0150.01429.48
7.1.40.0170.01429.34
7.1.30.0170.01429.36
7.1.20.0190.01229.51
7.1.10.0090.01115.92
7.1.00.0090.00916.09
7.0.330.0040.00715.30
7.0.320.0040.01115.62
7.0.310.0040.00415.54
7.0.300.0050.00515.35
7.0.290.0000.01015.67
7.0.280.0100.00615.32
7.0.270.0040.01115.54
7.0.260.0060.00915.15
7.0.250.0080.00916.90
7.0.240.0040.01316.76
7.0.230.0090.00916.89
7.0.220.0120.00916.85
7.0.210.0100.00915.88
7.0.200.0110.00616.01
7.0.190.0090.00715.82
7.0.180.0090.01015.64
7.0.170.0090.01015.69
7.0.160.0070.01115.58
7.0.150.0270.01015.65
7.0.140.0070.01215.67
7.0.130.0090.01015.77
7.0.120.0070.00915.79
7.0.110.0080.00815.75
7.0.100.0100.00815.65
7.0.90.0060.01115.70
7.0.80.0080.00715.76
7.0.70.0090.01215.63
7.0.60.0090.00715.59
7.0.50.0060.01115.74
7.0.40.0090.01115.31
7.0.30.0070.01115.35
7.0.20.0050.01015.21
7.0.10.0100.00715.21
7.0.00.0060.00815.26
5.6.400.0000.01214.29
5.6.390.0060.00914.27
5.6.380.0070.00714.16
5.6.370.0030.00614.14
5.6.360.0030.01414.00
5.6.350.0070.00714.40
5.6.340.0030.01614.36
5.6.330.0040.01114.00
5.6.320.0000.01314.25
5.6.310.0060.00914.27
5.6.300.0080.05719.10
5.6.290.0080.05019.13
5.6.280.0140.04519.21
5.6.270.0110.04719.19
5.6.260.0080.05219.22
5.6.250.0080.04619.15
5.6.240.0080.04719.12
5.6.230.0060.04819.20
5.6.220.0090.04119.18
5.6.210.0120.03919.25
5.6.200.0070.04819.13
5.6.190.0090.04419.11
5.6.180.0080.04519.08
5.6.170.0130.04419.17
5.6.160.0080.04319.14
5.6.150.0110.04419.22
5.6.140.0060.05119.22
5.6.130.0150.04719.15
5.6.120.0100.04919.16
5.6.110.0090.04519.18
5.6.100.0070.04719.05
5.6.90.0070.04419.22
5.6.80.0090.04118.69
5.6.70.0110.03818.64
5.6.60.0110.04018.74
5.6.50.0120.04018.75
5.6.40.0070.04718.69
5.6.30.0090.04318.65
5.6.20.0080.04418.60
5.6.10.0120.04418.62
5.6.00.0080.04718.66
5.5.380.0100.04917.49
5.5.370.0070.05217.40
5.5.360.0090.06017.64
5.5.350.0000.05717.43
5.5.340.0200.04118.04
5.5.330.0100.05317.80
5.5.320.0130.05017.78
5.5.310.0070.05218.03
5.5.300.0070.05417.94
5.5.290.0100.05517.96
5.5.280.0070.05917.80
5.5.270.0070.06717.71
5.5.260.0060.05718.02
5.5.250.0000.06017.59
5.5.240.0030.05217.38
5.5.230.0070.05217.41
5.5.220.0100.05117.38
5.5.210.0130.06717.08
5.5.200.0030.07517.06
5.5.190.0000.06317.38
5.5.180.0130.04917.09
5.5.160.0150.06217.38
5.5.150.0100.06117.30
5.5.140.0100.07017.38
5.5.130.0070.05317.38
5.5.120.0070.06917.31
5.5.110.0110.04817.03
5.5.100.0110.05617.29
5.5.90.0100.05416.95
5.5.80.0170.04816.98
5.5.70.0130.06616.96
5.5.60.0000.05817.04
5.5.50.0000.05817.34
5.5.40.0060.05617.27
5.5.30.0030.07416.98
5.5.20.0110.04917.27
5.5.10.0070.07417.18
5.5.00.0140.04817.03
5.4.450.0130.05319.57
5.4.440.0140.05119.16
5.4.430.0130.04619.15
5.4.420.0070.05519.39
5.4.410.0070.05619.22
5.4.400.0070.04819.15
5.4.390.0070.05319.14
5.4.380.0130.04619.14
5.4.370.0100.06619.10
5.4.360.0100.07419.01
5.4.350.0100.04918.86
5.4.340.0070.05518.96
5.4.320.0100.06019.07
5.4.310.0100.06719.02
5.4.300.0100.06518.98
5.4.290.0070.06218.98
5.4.280.0030.07819.14
5.4.270.0070.06919.00
5.4.260.0060.07418.95
5.4.250.0100.05919.01
5.4.240.0100.06319.04
5.4.230.0100.06118.82
5.4.220.0060.05518.94
5.4.210.0070.05618.85
5.4.200.0170.05719.04
5.4.190.0030.05619.14
5.4.180.0070.04919.09
5.4.170.0100.04819.13
5.4.160.0200.05619.02
5.4.150.0040.06019.13
5.4.140.0100.04516.70
5.4.130.0030.05116.67
5.4.120.0100.04616.61
5.4.110.0100.04616.48
5.4.100.0000.05216.41
5.4.90.0100.04216.42
5.4.80.0070.04416.57
5.4.70.0130.05916.57
5.4.60.0060.06716.63
5.4.50.0090.04416.65
5.4.40.0100.05316.63
5.4.30.0100.06016.64
5.4.20.0060.06616.45
5.4.10.0070.05316.64
5.4.00.0090.05316.16
5.3.290.0030.06214.73
5.3.280.0130.04514.88
5.3.270.0070.06914.79
5.3.260.0030.07314.66
5.3.250.0070.05914.78
5.3.240.0070.05014.73
5.3.230.0100.04614.87
5.3.220.0070.04914.74
5.3.210.0130.04614.86
5.3.200.0030.05014.85
5.3.190.0070.04714.84
5.3.180.0030.07014.82
5.3.170.0100.06314.83
5.3.160.0090.05714.73
5.3.150.0110.05014.83
5.3.140.0100.05914.83
5.3.130.0070.06914.81
5.3.120.0100.06514.81
5.3.110.0100.05014.85
5.3.100.0030.06214.33
5.3.90.0160.04314.23
5.3.80.0100.06014.07
5.3.70.0100.05814.24
5.3.60.0050.05214.30
5.3.50.0030.06514.16
5.3.40.0070.05113.94
5.3.30.0100.06114.11
5.3.20.0030.06913.57
5.3.10.0070.04413.71
5.3.00.0130.05113.87
5.2.170.0100.03811.43
5.2.160.0050.05311.27
5.2.150.0100.03811.45
5.2.140.0090.05011.34
5.2.130.0030.05511.21
5.2.120.0060.04111.38
5.2.110.0100.03211.32
5.2.100.0070.04711.09
5.2.90.0040.04211.23
5.2.80.0070.03811.35
5.2.70.0070.05111.21
5.2.60.0060.03811.05
5.2.50.0000.04311.20
5.2.40.0070.03511.13
5.2.30.0020.05011.15
5.2.20.0070.03911.08
5.2.10.0080.03411.05
5.2.00.0070.03610.58
5.1.60.0070.0309.99
5.1.50.0070.04110.28
5.1.40.0110.04010.16
5.1.30.0030.05010.55
5.1.20.0060.03710.24
5.1.10.0030.04010.16
5.1.00.0000.05210.05
5.0.50.0060.0348.81
5.0.40.0060.0338.51
5.0.30.0030.0578.40
5.0.20.0100.0308.40
5.0.10.0100.0328.48
5.0.00.0090.0478.26
4.4.90.0030.0266.73
4.4.80.0000.0236.73
4.4.70.0000.0256.73
4.4.60.0000.0236.73
4.4.50.0030.0206.73
4.4.40.0030.0336.73
4.4.30.0090.0186.73
4.4.20.0060.0226.73
4.4.10.0060.0286.73
4.4.00.0030.0396.73
4.3.110.0000.0316.73
4.3.100.0040.0286.73
4.3.90.0060.0256.73
4.3.80.0030.0426.73
4.3.70.0000.0306.73
4.3.60.0030.0276.73
4.3.50.0070.0246.73
4.3.40.0030.0366.73
4.3.30.0000.0246.73
4.3.20.0040.0216.73
4.3.10.0040.0216.73
4.3.00.0030.0226.73

preferences:
33.51 ms | 403 KiB | 5 Q