3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * A code sample which converts odd integers to word strings and returns the sum of the common factors of even numbers. * * The input is assumed to be a random list of integers. For the sum of the common factors of even numbers * only positive values are taken into consideration */ error_reporting(E_ALL); function exception_handler($exception) { echo get_class($exception). ' : ' . $exception->getMessage(), "\n"; } set_exception_handler('exception_handler'); //start $iB = new IntelligentBee(array(6,12,48, 7, 101, 67)); $iB->processOddNumbers(); $sum = $iB->processEvenNumbers(); echo PHP_EOL.'Sum of common factors: ' . $sum . PHP_EOL; //end class IntelligentBee{ const OneHundred = 100; const OneThousand = 1000; const OneMillion = 1000000; const OneBillion = 1000000000; protected $minCommonFactors = []; protected $oddNumbers = []; protected $evenNumbers = []; protected $startInt; protected $endInt; protected static $dictionary = array( 0=>'zero', 1=>'one', 2=>'two', 3=>'three', 4=>'four', 5=>'five', 6=>'six', 7=>'seven', 8=>'eight', 9=>'nine', 10=>'ten', 11=>'eleven', 12=>'twelve', 13=>'thirteen', 14=>'fourteen', 15=>'fifthteen', 16=>'sixteen', 17=>'seventeen', 18=>'eighteen', 19=>'nineteen', 20=>'twenty', 30=>'thirty', 40=>'forty', 50=>'fifty', 60=>'sixty', 70=>'seventy', 80=>'eighty', 90=>'ninety', self::OneHundred=>'hundred', self::OneThousand=>'thousand', self::OneMillion=>'million', self::OneBillion=>'billion'//might get into memory issues ); public function __construct(array $arrayOfIntegers){ $startInt = min($arrayOfIntegers); $endInt = max($arrayOfIntegers); if(abs($startInt) > self::OneBillion || abs($startInt) > self::OneBillion){ throw new OutOfRangeException(sprintf("We can only process numbers between %s and %s! %s provided!", self::OneBillion, self::OneBillion, $startInt)); } if(abs($endInt) > self::OneBillion || abs($endInt) > self::OneBillion){ throw new OutOfRangeException(sprintf("We can only process numbers between %s and %s! %s provided!", self::OneBillion, self::OneBillion, $startInt)); } $this->startInt = $startInt; $this->endInt = $endInt; //split it into 2 arrays, one for odd numbers and one for even numbers to make it easier to perform each task $this->splitNumbers($arrayOfIntegers); } public function processOddNumbers() { foreach($this->oddNumbers as $oddNumber){ echo $this->convertIntToWord($oddNumber).PHP_EOL; } } public function processEvenNumbers() { $commonFactors = $this->calcFactors($this->getClosestEvenToZero($this->startInt, $this->endInt, $this->evenNumbers)); foreach($this->evenNumbers as $evenNumber){ $commonFactors = $this->checkIfHasCommonFactors($evenNumber, $commonFactors); } return array_sum($commonFactors); } protected function splitNumbers(array $arrayOfIntegers){ foreach($arrayOfIntegers as $int){ if($int & 1){ //odd number $this->oddNumbers[] = $int; }else{ //even number $this->evenNumbers[] = $int; } } sort($this->evenNumbers);//sort the even numbers as it helps calculate the closest number to 0 } protected function getClosestEvenToZero($startInt, $endInt, $evenNumbers){ $dist = abs($evenNumbers[0]); $idx = 0; $totalNo = count($evenNumbers); for($i=0;$i<$totalNo;$i++){ $newDist = abs($evenNumbers[$i]); if($newDist < $dist){ $idx = $i; $dist = $newDist; } if($dist == 0){//only if evenNumbers is sorted return $evenNumbers[$idx]; } } return $evenNumbers[$idx]; } protected function checkIfHasCommonFactors($int, array $minCommonFactors){ $int = abs($int); $cF = $this->calcFactors($int); return array_intersect($cF, $minCommonFactors); } private function calcFactors($int) { $int = abs($int); $cF = []; $max = $int/2; for($i=1;$i<=$max;$i++){ if(0 === $int%$i){ $cF[] = $i; } } $cF[] = $int; return $cF; } protected function convertIntToWord($int) { $word = ''; if($int < 0){ $word .= 'minus ' . $this->convertIntToWord(abs($int)); } if(0 <= $int && $int < 20){ return self::$dictionary[$int]; } if(20 <= $int && $int < self::OneHundred){ return $this->processTens($int); } if (self::OneHundred <= $int && $int < self::OneThousand) { return $this->processHundreds($int); } if(self::OneThousand <= $int && $int < self::OneMillion){ return $this->processBigNumber($int, self::OneThousand); } if(self::OneMillion <= $int && $int < self::OneBillion){ return $this->processBigNumber($int, self::OneMillion); }else{ return $this->processBigNumber($int, self::OneBillion); } } protected function processTens($int) { $tens = intval($int/10)*10; $units = $int%10; $conv = self::$dictionary[$tens]; $conv .= $units > 0 ? '-'.self::$dictionary[$units] : ''; return $conv; } protected function processHundreds($int) { $hundreds = intval($int/100); $remainder = $int%100; $conv = self::$dictionary[$hundreds] . ' ' . self::$dictionary[self::OneHundred]; $conv .= $remainder > 0 ? " and " . $this->convertIntToWord($remainder) : ''; return $conv; } protected function processBigNumber($int, $baseUnit) { $nrBaseUnits = intval($int/$baseUnit); $remainder = $int%$baseUnit; $conv = $this->convertIntToWord($nrBaseUnits) . ' ' . self::$dictionary[$baseUnit]; $conv .= $remainder <= 0 ? "" : ($remainder < 100 ? " and " : ", ") . $this->convertIntToWord($remainder); return $conv; } }

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).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.2.00.0060.00919.47
7.1.100.0030.00917.91
7.1.70.0000.00917.18
7.1.60.0130.01319.46
7.1.50.0000.01417.05
7.1.00.0070.06322.48
7.0.200.0000.00816.93
7.0.140.0070.06721.99
7.0.100.0230.06320.05
7.0.90.0030.08720.13
7.0.80.0100.04020.06
7.0.70.0230.04319.98
7.0.60.0030.08019.94
7.0.50.0100.04320.31
7.0.40.0130.06719.96
7.0.30.0070.08320.09
7.0.20.0070.07319.96
7.0.10.0100.07020.17
7.0.00.0130.06020.18
5.6.280.0030.07320.88
5.6.250.0130.07720.77
5.6.240.0070.05020.68
5.6.230.0000.07720.74
5.6.220.0070.06320.64
5.6.210.0030.08020.52
5.6.200.0100.05321.14
5.6.190.0130.09021.10
5.6.180.0170.04021.16
5.6.170.0100.07721.18
5.6.160.0100.07021.11
5.6.150.0130.06321.04
5.6.140.0200.03721.18
5.6.130.0100.04321.13
5.6.120.0100.07721.13
5.6.110.0100.06721.05
5.6.100.0070.09021.06
5.6.90.0100.03720.99
5.6.80.0000.05020.58
5.6.70.0130.04020.48
5.6.60.0070.07720.39
5.6.50.0100.08320.55
5.6.40.0070.05320.47
5.6.30.0070.04720.49
5.6.20.0070.04320.41
5.6.10.0070.04020.36
5.6.00.0030.04320.47
5.5.380.0000.05320.41
5.5.370.0030.05320.41
5.5.360.0130.07320.43
5.5.350.0100.04320.42
5.5.340.0130.07020.79
5.5.330.0070.08320.88
5.5.320.0030.08720.70
5.5.310.0070.08020.95
5.5.300.0000.05020.95
5.5.290.0070.03720.90
5.5.280.0000.07320.97
5.5.270.0030.08020.94
5.5.260.0130.03020.95
5.5.250.0030.07720.69
5.5.240.0030.05320.31
5.5.230.0070.07720.26
5.5.220.0130.07020.27
5.5.210.0130.07320.34
5.5.200.0070.08720.25
5.5.190.0000.05320.27
5.5.180.0170.06320.33
5.5.160.0170.06020.28
5.5.150.0100.08320.31
5.5.140.0070.08020.30
5.5.130.0100.04720.29
5.5.120.0000.06320.28
5.5.110.0130.03720.13
5.5.100.0100.07720.01
5.5.90.0070.08020.13
5.5.80.0000.04020.12
5.5.70.0070.05320.07
5.5.60.0100.07720.12
5.5.50.0070.06320.21
5.5.40.0030.08020.20
5.5.30.0100.04320.15
5.5.20.0030.08320.15
5.5.10.0030.08020.13
5.5.00.0130.03020.13
5.4.450.0070.04719.36
5.4.440.0030.07019.50
5.4.430.0070.06319.37
5.4.420.0030.07719.41
5.4.410.0030.06319.32
5.4.400.0100.03018.95
5.4.390.0030.07319.15
5.4.380.0000.08319.09
5.4.370.0170.06719.12
5.4.360.0130.07319.09
5.4.350.0130.04018.91
5.4.340.0030.06319.13
5.4.320.0070.08319.13
5.4.310.0030.05718.90
5.4.300.0030.05019.02
5.4.290.0070.05019.13
5.4.280.0000.06719.12
5.4.270.0130.05019.13
5.4.260.0030.08019.16
5.4.250.0030.06319.14
5.4.240.0070.03718.95
5.4.230.0130.07019.05
5.4.220.0070.03318.87
5.4.210.0070.07319.12
5.4.200.0000.08018.96
5.4.190.0070.07718.86
5.4.180.0070.03319.15
5.4.170.0000.07719.07
5.4.160.0070.04319.02
5.4.150.0030.07018.83
5.4.140.0000.07016.47
5.4.130.0100.06716.50
5.4.120.0030.07016.48
5.4.110.0030.04316.29
5.4.100.0100.06716.54
5.4.90.0100.07016.39
5.4.80.0070.07316.57
5.4.70.0130.06316.51
5.4.60.0170.05716.53
5.4.50.0000.05316.48
5.4.40.0170.06016.46
5.4.30.0100.07316.34
5.4.20.0100.07716.36
5.4.10.0030.04016.36
5.4.00.0070.03315.77
5.3.290.0100.06714.70
5.3.280.0030.03714.73
5.3.270.0030.06014.70
5.3.260.0130.06314.59
5.3.250.0130.07014.54
5.3.240.0000.05314.74
5.3.230.0100.07014.55
5.3.220.0100.04014.49
5.3.210.0030.07014.61
5.3.200.0000.04014.60
5.3.190.0100.05014.59
5.3.180.0000.04714.49
5.3.170.0070.06714.58
5.3.160.0070.07314.51
5.3.150.0100.05314.68
5.3.140.0000.06014.54
5.3.130.0100.07314.68
5.3.120.0030.04714.51
5.3.110.0130.06714.52
5.3.100.0030.06714.03
5.3.90.0130.06313.94
5.3.80.0070.07713.93
5.3.70.0000.08014.11
5.3.60.0070.05313.98
5.3.50.0070.03714.04
5.3.40.0000.06014.09
5.3.30.0030.08013.97
5.3.20.0070.07313.79
5.3.10.0100.03713.59
5.3.00.0070.07313.56