3v4l.org

run code in 300+ PHP versions simultaneously
<?php function wordsToArr2($str) { $words = [$str]; while ($pos =strpos ( $str , ' ')) { $str=substr($str,($pos+1)); $words[] =$str; } return $words; } function wordsToArr3($str) { $base = array_reverse(explode(' ', $str)); $words = [$placeholder = array_shift($base)]; foreach ($base as $word) { $words[] = $placeholder = $word . ' ' . $placeholder; } return array_reverse($words); } function wordsToArr4($str) { $base = explode(' ', $str); end($base); $l = key($base); $words = [$placeholder = $base[$l--]]; for ($i=$l; $i>=0;$i--) { $words[] = $placeholder = $base[$i] . ' ' . $placeholder; } return array_reverse($words); } function wordsToArr5($str) { $base = str_word_count($str, 1); $l = count($base)-1; $words = [$placeholder = $base[$l--]]; for ($i=$l; $i>=0;$i--) { $words[] = $placeholder = $base[$i] . ' ' . $placeholder; } return array_reverse($words); } function bench($callback, $arg, $runs = 100) { $t = 0; $now = microtime(true); for ($i=0; $i<$runs; $i++) { $callback($arg); } $t += (microtime(true) - $now); return [number_format($t, 8), $i]; } $sentence = 'The quick brown fox jumps over the lazy dog'; list($t, $i) = bench('wordsToArr2', $sentence); echo 'strpos '. $i .' times in: ' . $t . '/sec' . PHP_EOL; list($t, $i) = bench('wordsToArr3', $sentence); echo 'concat '. $i .' times in: ' . $t . '/sec' . PHP_EOL; list($t, $i) = bench('wordsToArr4', $sentence); echo 'end/key '. $i .' times in: ' . $t . '/sec' . PHP_EOL; list($t, $i) = bench('wordsToArr5', $sentence); echo 'count-- '. $i .' times in: ' . $t . '/sec' . PHP_EOL;

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)
7.3.10.0160.00516.29
7.3.00.0180.00616.60
7.2.130.0160.00516.45
7.2.120.0130.01016.77
7.2.110.0100.01016.67
7.2.100.0160.00616.72
7.2.90.0160.00916.65
7.2.80.0120.00616.53
7.2.70.0110.00516.52
7.2.60.0070.01116.58
7.2.50.0090.01016.64
7.2.40.0070.01116.63
7.2.30.0060.01216.76
7.2.20.0110.00716.46
7.2.10.0100.00816.67
7.2.00.0050.01216.79
7.1.250.0100.01315.54
7.1.230.0080.00415.35
7.1.220.0070.00715.42
7.1.210.0090.00615.41
7.1.200.0100.00315.31
7.1.190.0090.00515.40
7.1.180.0120.00615.65
7.1.170.0070.00815.50
7.1.160.0130.00315.51
7.1.150.0100.00715.70
7.1.140.0120.00315.41
7.1.130.0100.00315.56
7.1.120.0030.01315.34
7.1.110.0030.01115.41
7.1.100.0030.01215.56
7.1.90.0060.01015.40
7.1.80.0080.00815.70
7.1.70.0100.00715.52
7.1.60.0170.01733.33
7.1.50.0270.00733.36
7.1.40.0200.01333.72
7.1.30.0260.01033.38
7.1.20.0190.01033.32
7.1.10.0130.00015.43
7.1.00.0050.01015.41
5.6.380.0060.01114.42

preferences:
35.66 ms | 403 KiB | 5 Q