3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace JoshDiFabio; /* * Note that I have included the interface and both concrete classes in a single file purely to * simplify the process of distributing and reviewing the code. Ordinarily I would use a separate * file for each class according to PSR-0 and use an autoloader to load them. * * This implementation assumes the following integer => numeral mappings which might not always be * considered implicit: 4 => IV, 9 => IX, 40 => XL, 90 => XC, 400 => CD, 900 => CM. This is because, * according to Wikipedia, I can precede V and X, X can precede L and C, etc. * * I have assumed that the rules of Roman Numerals are static; hence the fast, graceful but fairly * inflexible (in the sense that it can't be configured) solution. * * 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 => self::ONE, 4 => self::FOUR, 5 => self::FIVE, 9 => self::NINE, 10 => self::TEN, 40 => self::FOURTY, 50 => self::FIFTY, 90 => self::NINETY, 100 => self::ONE_HUNDRED, 400 => self::FOUR_HUNDRED, 500 => self::FIVE_HUNDRED, 900 => self::NINE_HUNDRED, 1000 => self::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) { /* * append matching numeral to output string and deduct its integer value from * input number */ $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.60.0120.00918.52
8.3.50.0110.00922.05
8.3.40.0090.00618.76
8.3.30.0150.00419.23
8.3.20.0000.00720.29
8.3.10.0080.00023.65
8.3.00.0050.00319.56
8.2.180.0120.00918.21
8.2.170.0110.00722.96
8.2.160.0100.00320.35
8.2.150.0070.00024.18
8.2.140.0060.00324.66
8.2.130.0040.00426.16
8.2.120.0050.00320.94
8.2.110.0030.00622.30
8.2.100.0090.00317.99
8.2.90.0040.00419.30
8.2.80.0080.00017.97
8.2.70.0050.00517.63
8.2.60.0060.00317.80
8.2.50.0040.00418.07
8.2.40.0000.00718.16
8.2.30.0070.00418.36
8.2.20.0060.00317.86
8.2.10.0040.00418.09
8.2.00.0040.00417.71
8.1.280.0070.00725.92
8.1.270.0060.00323.96
8.1.260.0080.00026.35
8.1.250.0070.00028.09
8.1.240.0030.00723.85
8.1.230.0040.00719.00
8.1.220.0040.00417.77
8.1.210.0000.00818.77
8.1.200.0000.00917.38
8.1.190.0040.00417.13
8.1.180.0040.00418.10
8.1.170.0040.00418.65
8.1.160.0040.00422.05
8.1.150.0000.00718.87
8.1.140.0000.00817.37
8.1.130.0000.00717.90
8.1.120.0050.00217.46
8.1.110.0030.00617.37
8.1.100.0000.00817.49
8.1.90.0030.00317.52
8.1.80.0080.00217.45
8.1.70.0040.00417.38
8.1.60.0030.00517.56
8.1.50.0040.00417.59
8.1.40.0040.00417.59
8.1.30.0040.00417.64
8.1.20.0050.00217.53
8.1.10.0080.00017.68
8.1.00.0000.00717.51
8.0.300.0040.00418.77
8.0.290.0000.01016.75
8.0.280.0040.00418.43
8.0.270.0040.00417.30
8.0.260.0070.00017.30
8.0.250.0020.00516.95
8.0.240.0030.00317.01
8.0.230.0030.00316.97
8.0.220.0060.00316.95
8.0.210.0040.00416.84
8.0.200.0000.00617.02
8.0.190.0040.00416.90
8.0.180.0040.00416.97
8.0.170.0040.00416.94
8.0.160.0000.00816.84
8.0.150.0030.00316.81
8.0.140.0000.00716.90
8.0.130.0000.00613.44
8.0.120.0000.00716.85
8.0.110.0050.00316.93
8.0.100.0000.00716.97
8.0.90.0000.00816.90
8.0.80.0100.00716.88
8.0.70.0080.00016.90
8.0.60.0000.00716.93
8.0.50.0000.00816.92
8.0.30.0140.00617.23
8.0.20.0100.00917.42
8.0.10.0040.00417.01
8.0.00.0090.00816.98
7.4.330.0050.00015.02
7.4.320.0030.00316.65
7.4.300.0060.00016.61
7.4.290.0070.00016.55
7.4.280.0000.00816.63
7.4.270.0030.00316.68
7.4.260.0080.00016.52
7.4.250.0040.00416.46
7.4.240.0050.00216.62
7.4.230.0040.00416.30
7.4.220.0090.01616.48
7.4.210.0100.01016.61
7.4.200.0040.00416.65
7.4.160.0040.01116.74
7.4.150.0100.01017.40
7.4.140.0050.01717.86
7.4.130.0140.00516.49
7.4.120.0080.00816.54
7.4.110.0130.00616.38
7.4.100.0160.00316.73
7.4.90.0100.00616.43
7.4.80.0090.01519.39
7.4.70.0140.00316.67
7.4.60.0100.01016.58
7.4.50.0000.00916.44
7.4.40.0100.00716.56
7.4.30.0140.00716.63
7.4.00.0070.01014.94
7.3.330.0060.00013.27
7.3.320.0000.00513.22
7.3.310.0070.00016.37
7.3.300.0000.00816.42
7.3.290.0120.00316.42
7.3.280.0070.01316.38
7.3.270.0030.01317.40
7.3.260.0150.01116.68
7.3.250.0140.00316.44
7.3.240.0070.01116.69
7.3.230.0190.00316.39
7.3.210.0120.00416.52
7.3.200.0090.01219.39
7.3.190.0090.01216.50
7.3.180.0090.00616.70
7.3.170.0070.01016.63
7.3.160.0160.00016.49
7.3.120.0070.00715.16
7.3.10.0040.00816.56
7.3.00.0090.00516.52
7.2.330.0100.00716.68
7.2.320.0080.00816.93
7.2.310.0140.00316.89
7.2.300.0170.00016.46
7.2.290.0120.00416.60
7.2.130.0070.00716.81
7.2.120.0060.01016.80
7.2.110.0080.00616.54
7.2.100.0080.00716.69
7.2.90.0070.00716.76
7.2.80.0040.01016.85
7.2.70.0080.00916.93
7.2.60.0080.00616.72
7.2.50.0090.00316.67
7.2.40.0060.01116.63
7.2.30.0060.00716.59
7.2.20.0070.00716.70
7.2.10.0080.00816.77
7.2.00.0030.01216.96
7.1.250.0050.01015.55
7.1.240.0040.00815.86
7.1.230.0110.00315.92
7.1.220.0000.01315.51
7.1.210.0000.01315.81
7.1.200.0090.00415.94
7.1.190.0000.01015.63
7.1.180.0040.01115.77
7.1.170.0060.00615.63
7.1.160.0030.01015.97
7.1.150.0000.00915.84
7.1.140.0070.00715.57
7.1.130.0040.00715.84
7.1.120.0000.01515.68
7.1.110.0060.00815.89
7.1.100.0000.01015.62
7.1.90.0000.01015.79
7.1.80.0070.00315.79
7.1.70.0070.00316.47
7.1.60.0020.01117.83
7.1.50.0070.01116.12
7.1.40.0090.00915.68
7.1.30.0050.00515.86
7.1.20.0060.00615.71
7.1.10.0080.00415.75
7.1.00.0070.03819.13
7.0.330.0030.01015.37
7.0.320.0040.01114.94
7.0.310.0040.01115.37
7.0.300.0070.00715.22
7.0.290.0030.00714.99
7.0.280.0040.00715.30
7.0.270.0030.01015.10
7.0.260.0030.00915.22
7.0.250.0100.00315.07
7.0.240.0110.00315.27
7.0.230.0070.00715.28
7.0.220.0100.00315.43
7.0.210.0110.00315.04
7.0.200.0030.01115.99
7.0.190.0040.00815.17
7.0.180.0040.01115.36
7.0.170.0070.00715.42
7.0.160.0040.01115.30
7.0.150.0070.00715.34
7.0.140.0010.04318.65
7.0.130.0030.01315.46
7.0.120.0080.00315.37
7.0.110.0070.00715.34
7.0.100.0030.04017.71
7.0.90.0240.03917.68
7.0.80.0300.03717.64
7.0.70.0100.04217.58
7.0.60.0260.03917.68
7.0.50.0130.04317.97
7.0.40.0020.04716.71
7.0.30.0020.04216.59
7.0.20.0050.03016.66
7.0.10.0090.04216.77
7.0.00.0100.02516.68
5.6.380.0040.01114.65
5.6.370.0030.00714.22
5.6.360.0030.00914.11
5.6.350.0030.00914.36
5.6.340.0030.00913.89
5.6.330.0000.01114.43
5.6.320.0030.01214.49
5.6.310.0070.00314.24
5.6.300.0090.00314.27
5.6.290.0000.01414.09
5.6.280.0000.04417.82
5.6.270.0040.01114.07
5.6.260.0080.00514.71
5.6.250.0050.04517.57
5.6.240.0060.04317.47
5.6.230.0050.04517.40
5.6.220.0100.04517.45
5.6.210.0120.04217.52
5.6.200.0150.02717.54
5.6.190.0080.04217.59
5.6.180.0150.03817.71
5.6.170.0050.05017.68
5.6.160.0020.03217.59
5.6.150.0120.04017.57
5.6.140.0060.03417.71
5.6.130.0040.03617.56
5.6.120.0050.02317.67
5.6.110.0070.04417.61
5.6.100.0090.03617.64
5.6.90.0070.04717.63
5.6.80.0070.04317.23
5.6.70.0050.04317.42
5.6.60.0030.04917.34
5.6.50.0080.02617.22
5.6.40.0030.04517.37
5.6.30.0100.03817.30
5.6.20.0050.03517.29
5.6.10.0070.02317.36
5.6.00.0030.02317.28
5.5.380.0090.03015.70
5.5.370.0020.03015.87
5.5.360.0050.04015.71
5.5.350.0050.04415.75
5.5.340.0060.02515.98
5.5.330.0060.04215.94
5.5.320.0080.03016.17
5.5.310.0110.04115.93
5.5.300.0070.02316.06
5.5.290.0040.02915.96
5.5.280.0030.02415.83
5.5.270.0030.04116.09
5.5.260.0050.02415.82
5.5.250.0020.02615.94
5.5.240.0050.03815.68
5.5.230.0120.03515.66
5.5.220.0050.04115.56
5.5.210.0060.02615.66
5.5.200.0070.01815.64
5.5.190.0070.04315.65
5.5.180.0040.03315.65
5.5.170.0000.01211.36
5.5.160.0070.04115.64
5.5.150.0080.04115.78
5.5.140.0020.03215.69
5.5.130.0050.04515.59
5.5.120.0050.04315.56
5.5.110.0070.04015.58
5.5.100.0070.02915.52
5.5.90.0050.03015.43
5.5.80.0080.04515.41
5.5.70.0070.04315.50
5.5.60.0080.03815.59
5.5.50.0060.04115.64
5.5.40.0040.04215.63
5.5.30.0050.03715.52
5.5.20.0080.04315.52
5.5.10.0050.04315.65
5.5.00.0110.03615.46
5.4.450.0020.04215.24
5.4.440.0000.02515.31
5.4.430.0070.02315.15
5.4.420.0060.04515.08
5.4.410.0100.03515.12
5.4.400.0060.03615.02
5.4.390.0030.02815.05
5.4.380.0080.04015.05
5.4.370.0050.04015.08
5.4.360.0050.03814.96
5.4.350.0050.03914.96
5.4.340.0110.03314.99
5.4.330.0060.00310.97
5.4.320.0100.03215.00
5.4.310.0080.03815.13
5.4.300.0070.03714.95
5.4.290.0080.03815.01
5.4.280.0100.03014.90
5.4.270.0070.04215.03
5.4.260.0070.02315.04
5.4.250.0020.03814.90
5.4.240.0080.03615.01
5.4.230.0070.03415.17
5.4.220.0070.04614.97
5.4.210.0120.03714.97
5.4.200.0050.03215.06
5.4.190.0050.02415.06
5.4.180.0040.04614.97
5.4.170.0050.02715.03
5.4.160.0030.03515.03
5.4.150.0050.02315.02
5.4.140.0050.03313.71
5.4.130.0080.03413.57
5.4.120.0050.03213.69
5.4.110.0080.04513.70
5.4.100.0080.04013.64
5.4.90.0050.04013.70
5.4.80.0180.01313.76
5.4.70.0070.03313.84
5.4.60.0020.04213.72
5.4.50.0040.03213.60
5.4.40.0020.03313.69
5.4.30.0030.03313.65
5.4.20.0060.01813.82
5.4.10.0120.03313.73
5.4.00.0040.03113.49
5.3.290.0080.03012.77
5.3.280.0080.03112.76
5.3.270.0050.03312.73
5.3.260.0020.04312.75
5.3.250.0070.03512.82
5.3.240.0080.04112.77
5.3.230.0070.03812.73
5.3.220.0030.02412.72
5.3.210.0050.03712.77
5.3.200.0040.02712.71
5.3.190.0020.04212.74
5.3.180.0050.04112.76
5.3.170.0070.03812.70
5.3.160.0070.04012.73
5.3.150.0050.02712.79
5.3.140.0070.04112.80
5.3.130.0070.03212.78
5.3.120.0050.04012.74
5.3.110.0070.02012.76
5.3.100.0030.03112.53
5.3.90.0070.03712.47
5.3.80.0070.03912.45
5.3.70.0030.04612.41
5.3.60.0070.03212.45
5.3.50.0030.02312.37
5.3.40.0080.03612.47
5.3.30.0040.04012.37
5.3.20.0050.04012.34
5.3.10.0030.02712.36
5.3.00.0000.04412.23
5.2.170.0070.03311.03
5.2.160.0020.03510.99
5.2.150.0030.03511.00
5.2.140.0040.02810.95
5.2.130.0080.03011.02
5.2.120.0050.02510.99
5.2.110.0060.02811.01
5.2.100.0050.03310.93
5.2.90.0000.03311.04
5.2.80.0020.02111.00
5.2.70.0050.02710.99
5.2.60.0020.02710.94
5.2.50.0070.02010.99
5.2.40.0020.02310.85
5.2.30.0040.01610.96
5.2.20.0030.01810.93
5.2.10.0030.03010.89
5.2.00.0050.01910.77
5.1.60.0020.02010.46
5.1.50.0050.02810.46
5.1.40.0030.02810.43
5.1.30.0050.01910.65
5.1.20.0040.02410.58
5.1.10.0030.02210.50
5.1.00.0080.02310.48
5.0.50.0030.0139.74
5.0.40.0030.0259.66
5.0.30.0050.0159.56
5.0.20.0000.0219.56
5.0.10.0050.0169.56
5.0.00.0060.0279.56
4.4.90.0000.0139.56
4.4.80.0020.0159.56
4.4.70.0000.0139.56
4.4.60.0000.0219.56
4.4.50.0050.0159.56
4.4.40.0020.0159.56
4.4.30.0000.0139.56
4.4.20.0030.0109.56
4.4.10.0030.0189.56
4.4.00.0030.0159.56
4.3.110.0030.0159.56
4.3.100.0030.0139.56
4.3.90.0020.0139.56
4.3.80.0020.0189.56
4.3.70.0000.0189.56
4.3.60.0030.0159.56
4.3.50.0000.0209.56
4.3.40.0030.0159.56
4.3.30.0020.0109.56
4.3.20.0050.0179.56
4.3.10.0020.0179.56
4.3.00.0000.0169.56

preferences:
53.31 ms | 401 KiB | 5 Q