3v4l.org

run code in 300+ PHP versions simultaneously
<?php function sortKeywords(array $phrases) { if (empty($phrases)) { return $phrases; } // group by word count $byWordCount = array(); foreach ($phrases as $kw) { $kw = preg_replace('/\b+/', " ", $kw); $wordCount = count(explode(" ", $kw)); if (!array_key_exists($wordCount, $byWordCount)) { $byWordCount[$wordCount] = array(); } $byWordCount[$wordCount][] = $kw; } // within every group sort by average word length $sortByWordLen = function ($a, $b) { $getAverageWordLen = function (array $words) { $totalLen = 0; foreach ($words as $w) { $totalLen += strlen($w); } return $totalLen / (count($words)); }; $wordsA = explode(" ", $a); $wordsB = explode(" ", $b); $averageLenA = $getAverageWordLen($wordsA); $averageLenB = $getAverageWordLen($wordsB); if ($averageLenA < $averageLenB) { return 1; } else if ($averageLenA > $averageLenB) { return -1; } return 0; }; // sort keys (sentence lengths) DESC $keys = array_keys($byWordCount); sort($keys); $sortedLengths = array_reverse($keys); // built final array sorted by 1) sentence length DESC 2) averaged word length DESC $sortedKeywords = array(); foreach ($sortedLengths as $length) { $phrases = $byWordCount[$length]; usort($phrases, $sortByWordLen); $sortedKeywords = array_merge($sortedKeywords, $phrases); } return $sortedKeywords; } var_dump(sortKeywords(array( 'foo bar', 'foobar', 'foofoo bar' 'foo', 'foofoo barbar' )));

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)
5.4.350.0080.04212.04
5.4.340.0100.05612.04
5.4.320.0090.05212.54
5.4.310.0130.07812.54
5.4.300.0110.04912.54
5.4.290.0130.05512.54
5.4.280.0090.03912.43
5.4.270.0080.03912.43
5.4.260.0060.04712.43
5.4.250.0080.06412.43
5.4.240.0090.04812.43
5.4.230.0050.03912.42
5.4.220.0060.04212.43
5.4.210.0080.04412.42
5.4.200.0080.05212.42
5.4.190.0060.03912.42
5.4.180.0090.04312.42
5.4.170.0080.03812.42
5.4.160.0090.03612.42
5.4.150.0050.04012.42
5.4.140.0060.03612.11
5.4.130.0040.03712.09
5.4.120.0040.03812.05
5.4.110.0060.03512.05
5.4.100.0080.03312.05
5.4.90.0060.04212.05
5.4.80.0070.05112.05
5.4.70.0170.05612.04
5.4.60.0170.06312.05
5.4.50.0160.06512.05
5.4.40.0090.04112.03
5.4.30.0080.04012.03
5.4.20.0070.03812.03
5.4.10.0040.03912.03
5.4.00.0090.04511.52
5.3.290.0090.05312.80
5.3.280.0120.04912.71
5.3.270.0070.05412.72
5.3.260.0080.07012.72
5.3.250.0140.06412.72
5.3.240.0150.08912.72
5.3.230.0160.07712.70
5.3.220.0120.06612.68
5.3.210.0120.09012.68
5.3.200.0100.04012.68
5.3.190.0080.04612.68
5.3.180.0140.03312.68
5.3.170.0050.04312.68
5.3.160.0070.04412.67
5.3.150.0060.04212.67
5.3.140.0060.04112.67
5.3.130.0090.03912.66
5.3.120.0060.03812.66
5.3.110.0030.04212.66
5.3.100.0070.04312.15
5.3.90.0070.03612.13
5.3.80.0070.03512.12
5.3.70.0050.03912.12
5.3.60.0070.03712.11
5.3.50.0080.03412.05
5.3.40.0080.04112.05
5.3.30.0110.03312.01
5.3.20.0050.03711.80
5.3.10.0060.03711.76
5.3.00.0080.03711.74
5.2.170.0040.0449.22
5.2.160.0060.0329.23
5.2.150.0040.0349.22
5.2.140.0050.0369.22
5.2.130.0060.0339.18
5.2.120.0050.0359.18
5.2.110.0050.0399.19
5.2.100.0050.0409.18
5.2.90.0070.0369.18
5.2.80.0100.0549.18
5.2.70.0060.0369.18
5.2.60.0060.0349.13
5.2.50.0240.0649.10
5.2.40.0110.0459.08
5.2.30.0120.1129.05
5.2.20.0120.0579.04
5.2.10.0040.0368.95
5.2.00.0050.0348.81
5.1.60.0130.0358.10
5.1.50.0060.0368.09
5.1.40.0090.0648.07
5.1.30.0040.0308.42
5.1.20.0160.0458.45
5.1.10.0040.0298.17
5.1.00.0090.0338.17
5.0.50.0070.0196.64
5.0.40.0050.0266.50
5.0.30.0060.0326.31
5.0.20.0040.0216.29
5.0.10.0020.0216.26
5.0.00.0040.0306.25
4.4.90.0050.0144.78
4.4.80.0030.0164.75
4.4.70.0030.0174.75
4.4.60.0040.0174.75
4.4.50.0040.0154.77
4.4.40.0010.0284.71
4.4.30.0040.0184.75
4.4.20.0040.0184.85
4.4.10.0030.0174.85
4.4.00.0030.0274.76
4.3.110.0010.0194.67
4.3.100.0020.0164.67
4.3.90.0040.0154.63
4.3.80.0020.0294.58
4.3.70.0040.0154.63
4.3.60.0010.0164.63
4.3.50.0010.0174.63
4.3.40.0030.0254.53
4.3.30.0040.0143.30
4.3.20.0030.0153.29
4.3.10.0010.0173.23
4.3.00.0130.0277.74

preferences:
141.09 ms | 1386 KiB | 7 Q