3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace JoshDiFabio; /** * Note that I have included the interface and both classes in a single file purely to simplify the * review process. Ordinarily I would use a separate file for each class according to PSR-0 and use * an autoloader. * * Requires PHP >= 5.3.0 */ interface RomanNumeralGeneratorInterface { /** * @param int $number * @return string * @throws \InvalidArgumentException Argument not an integer or out of range * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public function generate($number); } /** * Class defining basic rules of roman numerals */ class RomanNumeral { const ONE = 'I'; const FOUR = 'IV'; const FIVE = 'V'; const NINE = 'IX'; const TEN = 'X'; const FOURTY = 'XL'; const FIFTY = 'L'; const NINETY = 'XC'; const ONE_HUNDRED = 'C'; const FOUR_HUNDRED = 'CD'; const FIVE_HUNDRED = 'D'; const NINE_HUNDRED = 'CM'; const ONE_THOUSAND = 'M'; /** * @return array * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public static function getIntegerToNumeralMap() { return array( 1 => RomanNumeral::ONE, 4 => RomanNumeral::FOUR, 5 => RomanNumeral::FIVE, 9 => RomanNumeral::NINE, 10 => RomanNumeral::TEN, 40 => RomanNumeral::FOURTY, 50 => RomanNumeral::FIFTY, 90 => RomanNumeral::NINETY, 100 => RomanNumeral::ONE_HUNDRED, 400 => RomanNumeral::FOUR_HUNDRED, 500 => RomanNumeral::FIVE_HUNDRED, 900 => RomanNumeral::NINE_HUNDRED, 1000 => RomanNumeral::ONE_THOUSAND, ); } } /** * Class for generating roman numeral strings based on integers */ class RomanNumeralGenerator implements RomanNumeralGeneratorInterface { /** * @param int $inputNumber * @return string * @throws \InvalidArgumentException Argument not an integer or out of range * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public function generate($inputNumber) { if (!is_integer($inputNumber)) { throw new \InvalidArgumentException('Provided argument is not an integer.'); } if (1 > $inputNumber || 3999 < $inputNumber) { throw new \InvalidArgumentException( 'The provided number is not within the allowed range of 1 to 3999.'); } $numeralString = ''; // get integer to numeral map and sort from largest to smallest $integerToNumeralMap = RomanNumeral::getIntegerToNumeralMap(); krsort($integerToNumeralMap); while (0 < $inputNumber) { foreach ($integerToNumeralMap as $_numeralAsInteger => $_numeral) { /* * if input number minus any already selected numerals is larger than the current * numeral, select the current numeral and deduct its integer value from the input * number */ if ($inputNumber >= $_numeralAsInteger) { $numeralString .= $_numeral; $inputNumber -= $_numeralAsInteger; break; } } } return $numeralString; } } $generator = new RomanNumeralGenerator; echo $generator->generate(3999);

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.40.0040.01118.84
8.3.30.0140.00019.04
8.3.20.0080.00020.12
8.3.10.0040.00422.14
8.3.00.0080.00022.46
8.2.170.0070.00722.96
8.2.160.0110.00420.87
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0050.00317.75
8.2.110.0030.00622.18
8.2.100.0120.00017.97
8.2.90.0090.00019.09
8.2.80.0050.00317.97
8.2.70.0030.00617.63
8.2.60.0000.00817.93
8.2.50.0000.00818.07
8.2.40.0080.00018.34
8.2.30.0090.00318.17
8.2.20.0000.00717.76
8.2.10.0040.00419.51
8.2.00.0050.00317.78
8.1.270.0050.00323.68
8.1.260.0090.00026.35
8.1.250.0040.00428.09
8.1.240.0060.00323.83
8.1.230.0070.00419.00
8.1.220.0000.00817.88
8.1.210.0060.00318.77
8.1.200.0100.00017.25
8.1.190.0040.00417.55
8.1.180.0040.00418.10
8.1.170.0060.00318.64
8.1.160.0030.00621.97
8.1.150.0050.00520.42
8.1.140.0070.00017.46
8.1.130.0030.00317.86
8.1.120.0070.00017.53
8.1.110.0000.00817.48
8.1.100.0000.00817.48
8.1.90.0040.00417.40
8.1.80.0040.00417.51
8.1.70.0030.00317.47
8.1.60.0030.00517.59
8.1.50.0030.00517.48
8.1.40.0000.00817.64
8.1.30.0080.00017.59
8.1.20.0000.00817.73
8.1.10.0050.00217.52
8.1.00.0030.00517.57
8.0.300.0040.00418.77
8.0.290.0000.00717.28
8.0.280.0030.00318.36
8.0.270.0070.00017.27
8.0.260.0030.00316.86
8.0.250.0040.00417.03
8.0.240.0050.00317.02
8.0.230.0040.00416.88
8.0.220.0070.00016.84
8.0.210.0070.00016.84
8.0.200.0030.00316.97
8.0.190.0000.00816.93
8.0.180.0000.00816.81
8.0.170.0000.00916.93
8.0.160.0040.00416.89
8.0.150.0000.00816.80
8.0.140.0040.00416.90
8.0.130.0000.00613.32
8.0.120.0040.00416.87
8.0.110.0040.00416.96
8.0.100.0000.00716.73
8.0.90.0040.00416.97
8.0.80.0040.01116.82
8.0.70.0040.00416.80
8.0.60.0050.00316.79
8.0.50.0040.00416.95
8.0.30.0090.01117.03
8.0.20.0120.00817.40
8.0.10.0050.00216.87
8.0.00.0130.00816.70
7.4.330.0050.00015.15
7.4.320.0000.00616.60
7.4.300.0060.00016.54
7.4.290.0000.00716.64
7.4.280.0070.00016.52
7.4.270.0000.00716.55
7.4.260.0000.00716.53
7.4.250.0000.00716.43
7.4.240.0050.00316.66
7.4.230.0000.00716.52
7.4.220.0100.00716.57
7.4.210.0080.00816.66
7.4.200.0020.00516.46
7.4.190.0000.00716.68
7.4.160.0050.01116.51
7.4.150.0000.01817.40
7.4.140.0160.00517.86
7.4.130.0110.00816.63
7.4.120.0120.00716.57
7.4.110.0100.00716.40
7.4.100.0120.01016.68
7.4.90.0130.00316.43
7.4.80.0060.01816.65
7.4.70.0080.00816.61
7.4.60.0060.01116.47
7.4.50.0050.00316.52
7.4.40.0000.01422.77
7.4.30.0140.00716.74
7.4.10.0040.01215.05
7.4.00.0090.00915.03
7.3.330.0030.00313.23
7.3.320.0000.00513.26
7.3.310.0000.00716.25
7.3.300.0000.00716.44
7.3.290.0060.00916.38
7.3.280.0070.01016.43
7.3.270.0090.00917.40
7.3.260.0160.00916.55
7.3.250.0100.01016.54
7.3.240.0100.00616.34
7.3.230.0090.00916.64
7.3.210.0120.00616.38
7.3.200.0060.01019.39
7.3.190.0120.00616.71
7.3.180.0070.01316.56
7.3.170.0040.01116.63
7.3.160.0090.01316.31
7.3.130.0060.00614.89
7.3.120.0070.00814.91
7.3.110.0050.01215.12
7.3.100.0070.00614.81
7.3.90.0050.00814.90
7.3.80.0070.00614.79
7.3.70.0050.00814.96
7.3.60.0080.00214.96
7.3.50.0060.00814.73
7.3.40.0020.01015.03
7.3.30.0050.00914.92
7.3.20.0080.00816.77
7.3.10.0050.01116.78
7.3.00.0030.00916.64
7.2.330.0140.00316.49
7.2.320.0030.01416.80
7.2.310.0100.01316.79
7.2.300.0100.01016.70
7.2.290.0100.00716.83
7.2.260.0060.01214.91
7.2.250.0040.01615.12
7.2.240.0050.01015.24
7.2.230.0120.00515.11
7.2.220.0050.01214.83
7.2.210.0020.01215.27
7.2.200.0110.00515.19
7.2.190.0030.01215.04
7.2.180.0080.00815.19
7.2.170.0050.00815.13
7.2.160.0080.00715.15
7.2.150.0050.01116.75
7.2.140.0100.00616.89
7.2.130.0030.00817.02
7.2.120.0100.00716.75
7.2.110.0050.00916.89
7.2.100.0050.01216.88
7.2.90.0070.00716.94
7.2.80.0070.00717.03
7.2.70.0070.01116.96
7.2.60.0070.00916.89
7.2.50.0070.00816.96
7.2.40.0030.01017.05
7.2.30.0050.00916.92
7.2.20.0070.00616.90
7.2.10.0070.00516.99
7.2.00.0090.00517.67
7.1.330.0080.00615.80
7.1.320.0090.00515.85
7.1.310.0090.00515.94
7.1.300.0030.01015.78
7.1.290.0030.00815.71
7.1.280.0050.00715.72
7.1.270.0080.00215.66
7.1.260.0030.01215.68
7.1.250.0030.01315.63
7.1.240.0070.00715.90
7.1.230.0080.00515.69
7.1.220.0100.00715.73
7.1.210.0050.00715.57
7.1.200.0070.00515.56
7.1.190.0020.00915.78
7.1.180.0090.00415.82
7.1.170.0050.00515.77
7.1.160.0070.00515.79
7.1.150.0070.00715.80
7.1.140.0050.00715.83
7.1.130.0050.00815.67
7.1.120.0050.00915.81
7.1.110.0070.00615.97
7.1.100.0050.00716.49
7.1.90.0060.00815.74
7.1.80.0080.00515.90
7.1.70.0020.00916.13
7.1.60.0070.01017.24
7.1.50.0080.00716.06
7.1.40.0110.00215.86
7.1.30.0020.01215.62
7.1.20.0100.00415.78
7.1.10.0050.00715.90
7.1.00.0080.03418.08
7.0.330.0030.00915.30
7.0.320.0070.00515.31
7.0.310.0080.00515.42
7.0.300.0070.00715.46
7.0.290.0050.00815.40
7.0.280.0090.00215.44
7.0.270.0070.00815.49
7.0.260.0070.00715.36
7.0.250.0040.00815.43
7.0.240.0060.00815.48
7.0.230.0070.00715.37
7.0.220.0070.00715.51
7.0.210.0060.00415.39
7.0.200.0070.00615.92
7.0.190.0050.00715.55
7.0.180.0070.00715.49
7.0.170.0050.01115.46
7.0.160.0020.00915.51
7.0.150.0020.00815.38
7.0.140.0070.02817.72
7.0.130.0040.01115.36
7.0.120.0030.01015.53
7.0.110.0060.00315.33
7.0.100.0060.00615.43
7.0.90.0070.00515.41
7.0.80.0030.01215.47
7.0.70.0050.00715.35
7.0.60.0080.03016.97
7.0.50.0060.01816.38
7.0.40.0070.02415.64
7.0.30.0160.02215.72
7.0.20.0090.02615.73
7.0.10.0150.03215.72
7.0.00.0040.02915.67
5.6.400.0060.00814.66
5.6.390.0070.00914.31
5.6.380.0030.01014.61
5.6.370.0040.01114.37
5.6.360.0050.00814.54
5.6.350.0080.00614.31
5.6.340.0110.00514.40
5.6.330.0060.00714.23
5.6.320.0040.00614.57
5.6.310.0070.00514.42
5.6.300.0080.00414.54
5.6.290.0040.00814.16
5.6.280.0050.03016.50
5.6.270.0020.01014.43
5.6.260.0080.00614.29
5.6.250.0060.00814.40
5.6.240.0010.01014.46
5.6.230.0030.01214.24
5.6.220.0080.00414.46
5.6.210.0110.02616.48
5.6.200.0070.02215.64
5.6.190.0040.01916.43
5.6.180.1270.02116.33
5.6.170.0120.02516.49
5.6.160.0060.02016.28
5.6.150.0080.02315.73
5.6.140.0080.01715.76
5.6.130.0040.01915.68
5.6.120.0080.03416.73
5.6.110.0120.02116.59
5.6.100.0060.03216.75
5.6.90.0070.03316.66
5.6.80.0070.02416.32
5.6.70.0760.02716.49
5.6.60.0100.00214.36
5.6.50.0090.00314.36
5.6.40.0040.00814.33
5.6.30.0040.00914.42
5.6.20.0030.01114.17
5.6.10.0040.01214.27
5.6.00.0040.01114.39
5.5.380.0050.00814.41
5.5.370.0060.00814.59
5.5.360.0040.00614.60
5.5.350.0020.03316.24
5.5.340.0070.02715.50
5.5.330.0090.02916.24
5.5.320.0140.03016.33
5.5.310.0140.03016.34
5.5.300.0090.01615.59
5.5.290.0060.01915.44
5.5.280.0070.03216.56
5.5.270.0040.03416.41
5.5.260.0050.03216.38
5.5.250.0070.01816.39
5.5.240.0070.02916.37
5.5.230.0040.00614.43
5.5.220.0060.00514.17
5.5.210.0050.00814.17
5.5.200.0050.00814.25
5.5.190.0050.01114.06
5.5.180.0020.01014.28
5.5.170.0070.00914.30
5.5.160.0020.01414.21
5.5.150.0020.01014.18
5.5.140.0070.00714.20
5.5.130.0070.00714.43
5.5.120.0020.01214.07
5.5.110.0080.00614.01
5.5.100.0070.00814.13
5.5.90.0040.00714.11
5.5.80.0060.00514.11
5.5.70.0030.01014.09
5.5.60.0030.01014.29
5.5.50.0040.00514.32
5.5.40.0050.00714.41
5.5.30.0070.00614.14
5.5.20.0030.01013.91
5.5.10.0030.01014.39
5.5.00.0040.01314.13
5.4.450.0700.01613.88
5.4.440.0590.01813.95
5.4.430.0650.01814.00
5.4.420.0580.01513.83
5.4.410.0570.01913.91
5.4.400.0630.01313.74
5.4.390.0560.01813.81
5.4.380.0580.01713.77
5.4.370.0690.01513.85
5.4.360.0670.01913.69
5.4.350.0570.01613.79
5.4.340.0560.01513.70
5.4.330.0020.00711.19
5.4.320.0550.01813.73
5.4.310.0580.01613.78
5.4.300.0700.01713.94
5.4.290.0670.01613.89
5.4.280.0680.01813.73
5.4.270.0600.01313.78
5.4.260.0560.01813.86
5.4.250.0560.02113.84
5.4.240.0670.02013.91
5.4.230.0700.01613.77
5.4.220.0700.01713.71
5.4.210.0620.01813.69
5.4.200.0520.01913.82
5.4.190.0650.01513.77
5.4.180.0590.01913.85
5.4.170.0680.01913.66
5.4.160.0620.01913.71
5.4.150.0620.01713.85
5.4.140.0630.01812.96
5.4.130.0590.02012.89
5.4.120.0610.01713.02
5.4.110.0570.01412.95
5.4.100.0580.02312.84
5.4.90.0580.01212.89
5.4.80.0600.02012.96
5.4.70.0630.01812.80
5.4.60.0650.01712.84
5.4.50.0660.01612.88
5.4.40.0610.01913.02
5.4.30.0630.01612.92
5.4.20.0630.01513.02
5.4.10.0630.01912.92
5.4.00.0630.01612.75
5.3.290.0630.02012.22
5.3.280.0640.01912.14
5.3.270.0670.01712.17
5.3.260.0540.01812.06
5.3.250.0620.01412.13
5.3.240.0640.01812.11
5.3.230.0620.01412.18
5.3.220.0640.01612.11
5.3.210.0600.02012.12
5.3.200.0650.01512.12
5.3.190.0640.01812.13
5.3.180.0660.01412.12
5.3.170.0620.02012.13
5.3.160.0560.01712.09
5.3.150.0650.01812.15
5.3.140.0620.01612.16
5.3.130.0640.01712.06
5.3.120.0640.01612.23
5.3.110.0640.01912.13
5.3.100.0660.01712.04
5.3.90.0640.01511.97
5.3.80.0640.02111.90
5.3.70.0560.02011.91
5.3.60.0640.01711.99
5.3.50.0520.01911.84
5.3.40.0550.01711.88
5.3.30.0660.01811.93
5.3.20.0610.01611.74
5.3.10.0590.01411.74
5.3.00.0550.01711.73
5.2.170.0270.04010.98
5.2.160.0070.02311.11
5.2.150.0000.03011.21
5.2.140.1070.03011.22
5.2.130.1330.03310.94
5.2.120.1400.02710.99
5.2.110.1270.03011.11
5.2.100.1430.03711.12
5.2.90.1400.03011.05
5.2.80.1130.02711.18
5.2.70.1730.02311.18
5.2.60.1430.02311.09
5.2.50.1400.02710.92
5.2.40.1100.02710.84
5.2.30.1130.02710.98
5.2.20.1070.02710.87
5.2.10.1100.03010.92
5.2.00.1100.03010.71
5.1.60.0170.0239.86
5.1.50.0030.0239.88
5.1.40.0000.0239.91
5.1.30.0030.02710.49
5.1.20.0730.02310.51
5.1.10.0930.02310.16
5.1.00.1200.0209.97
5.0.50.0500.0208.71
5.0.40.0600.0178.46
5.0.30.0530.0308.11
5.0.20.0670.0238.35
5.0.10.0500.0208.06
5.0.00.0330.0308.21
4.4.90.0470.0177.85
4.4.80.0470.0137.85
4.4.70.0000.0177.85
4.4.60.0000.0177.85
4.4.50.0030.0177.85
4.4.40.0000.0237.85
4.4.30.0000.0177.85
4.4.20.0030.0177.85
4.4.10.0000.0177.85
4.4.00.0270.0237.85
4.3.110.0530.0137.85
4.3.100.0430.0137.85
4.3.90.0500.0177.85
4.3.80.0530.0237.85
4.3.70.0500.0177.85
4.3.60.0400.0177.85
4.3.50.0500.0137.85
4.3.40.0530.0237.85
4.3.30.0000.0207.85
4.3.20.0000.0207.85
4.3.10.0030.0137.85
4.3.00.0000.0177.85

preferences:
43.76 ms | 400 KiB | 5 Q