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;
Output for 7.3.1
strpos 100 times in: 0.00017500/sec concat 100 times in: 0.00046301/sec end/key 100 times in: 0.00047898/sec count-- 100 times in: 0.00043988/sec
Output for 7.3.0
strpos 100 times in: 0.00043702/sec concat 100 times in: 0.00059915/sec end/key 100 times in: 0.00055885/sec count-- 100 times in: 0.00055003/sec
Output for 7.2.13
strpos 100 times in: 0.00022912/sec concat 100 times in: 0.00028300/sec end/key 100 times in: 0.02255392/sec count-- 100 times in: 0.00050402/sec
Output for 7.2.12
strpos 100 times in: 0.01390910/sec concat 100 times in: 0.00021601/sec end/key 100 times in: 0.00020909/sec count-- 100 times in: 0.00020194/sec
Output for 7.2.11
strpos 100 times in: 0.03218889/sec concat 100 times in: 0.00056982/sec end/key 100 times in: 0.02996397/sec count-- 100 times in: 0.00055504/sec
Output for 7.2.10
strpos 100 times in: 0.03395391/sec concat 100 times in: 0.00053310/sec end/key 100 times in: 0.00036097/sec count-- 100 times in: 0.00034404/sec
Output for 7.2.9
strpos 100 times in: 0.06165695/sec concat 100 times in: 0.00060391/sec end/key 100 times in: 0.00055814/sec count-- 100 times in: 0.00056100/sec
Output for 7.2.8
strpos 100 times in: 0.11802506/sec concat 100 times in: 0.00021195/sec end/key 100 times in: 0.00021100/sec count-- 100 times in: 0.00020099/sec
Output for 7.2.7
strpos 100 times in: 0.02560496/sec concat 100 times in: 0.00028300/sec end/key 100 times in: 0.00025010/sec count-- 100 times in: 0.00021815/sec
Output for 7.2.6
strpos 100 times in: 0.03924799/sec concat 100 times in: 0.00065017/sec end/key 100 times in: 0.00068998/sec count-- 100 times in: 0.00068307/sec
Output for 7.2.5
strpos 100 times in: 0.04076481/sec concat 100 times in: 0.00046015/sec end/key 100 times in: 0.00044799/sec count-- 100 times in: 0.00045013/sec
Output for 7.2.4
strpos 100 times in: 0.05758691/sec concat 100 times in: 0.00051999/sec end/key 100 times in: 0.00053191/sec count-- 100 times in: 0.00053596/sec
Output for 7.2.3
strpos 100 times in: 0.03220701/sec concat 100 times in: 0.00059581/sec end/key 100 times in: 0.00055504/sec count-- 100 times in: 0.00054693/sec
Output for 7.2.2
strpos 100 times in: 0.00017285/sec concat 100 times in: 0.00025797/sec end/key 100 times in: 0.10365701/sec count-- 100 times in: 0.00047398/sec
Output for 7.2.1
strpos 100 times in: 0.00016689/sec concat 100 times in: 0.00020885/sec end/key 100 times in: 0.02055502/sec count-- 100 times in: 0.00024891/sec
Output for 7.2.0
strpos 100 times in: 0.00016499/sec concat 100 times in: 0.00020695/sec end/key 100 times in: 0.00020099/sec count-- 100 times in: 0.00019717/sec
Output for 7.1.25
strpos 100 times in: 0.00033903/sec concat 100 times in: 0.00040913/sec end/key 100 times in: 0.00038886/sec count-- 100 times in: 0.00039506/sec
Output for 7.1.23
strpos 100 times in: 0.00017715/sec concat 100 times in: 0.00021815/sec end/key 100 times in: 0.00019312/sec count-- 100 times in: 0.00019503/sec
Output for 7.1.22
strpos 100 times in: 0.00015998/sec concat 100 times in: 0.00021505/sec end/key 100 times in: 0.00020695/sec count-- 100 times in: 0.00020790/sec
Output for 7.1.21
strpos 100 times in: 0.00021815/sec concat 100 times in: 0.00029898/sec end/key 100 times in: 0.00027800/sec count-- 100 times in: 0.00029206/sec
Output for 7.1.20
strpos 100 times in: 0.00013900/sec concat 100 times in: 0.00017190/sec end/key 100 times in: 0.00015807/sec count-- 100 times in: 0.00016308/sec
Output for 7.1.19
strpos 100 times in: 0.00010300/sec concat 100 times in: 0.00016689/sec end/key 100 times in: 0.00021100/sec count-- 100 times in: 0.00015807/sec
Output for 7.1.18
strpos 100 times in: 0.00010204/sec concat 100 times in: 0.00017691/sec end/key 100 times in: 0.00016284/sec count-- 100 times in: 0.00015783/sec
Output for 7.1.17
strpos 100 times in: 0.00016499/sec concat 100 times in: 0.00018191/sec end/key 100 times in: 0.00019383/sec count-- 100 times in: 0.00017595/sec
Output for 7.1.16
strpos 100 times in: 0.00019097/sec concat 100 times in: 0.00026798/sec end/key 100 times in: 0.00025201/sec count-- 100 times in: 0.00024986/sec
Output for 7.1.15
strpos 100 times in: 0.00010109/sec concat 100 times in: 0.00025797/sec end/key 100 times in: 0.00016499/sec count-- 100 times in: 0.00016308/sec
Output for 7.1.14
strpos 100 times in: 0.00011897/sec concat 100 times in: 0.00016308/sec end/key 100 times in: 0.00013900/sec count-- 100 times in: 0.00024009/sec
Output for 7.1.13
strpos 100 times in: 0.00016212/sec concat 100 times in: 0.00020194/sec end/key 100 times in: 0.00026894/sec count-- 100 times in: 0.00019193/sec
Output for 7.1.12
strpos 100 times in: 0.00019908/sec concat 100 times in: 0.00026703/sec end/key 100 times in: 0.00031090/sec count-- 100 times in: 0.00033092/sec
Output for 7.1.11
strpos 100 times in: 0.00012803/sec concat 100 times in: 0.00018978/sec end/key 100 times in: 0.00016809/sec count-- 100 times in: 0.00018096/sec
Output for 7.1.10
strpos 100 times in: 0.00010395/sec concat 100 times in: 0.00018597/sec end/key 100 times in: 0.00013423/sec count-- 100 times in: 0.00013995/sec
Output for 7.1.9
strpos 100 times in: 0.00021601/sec concat 100 times in: 0.00027013/sec end/key 100 times in: 0.00024796/sec count-- 100 times in: 0.00031614/sec
Output for 7.1.8
strpos 100 times in: 0.00022721/sec concat 100 times in: 0.00031590/sec end/key 100 times in: 0.00029397/sec count-- 100 times in: 0.00027990/sec
Output for 7.1.7
strpos 100 times in: 0.00033116/sec concat 100 times in: 0.00035000/sec end/key 100 times in: 0.00027895/sec count-- 100 times in: 0.00016499/sec
Output for 7.1.6
strpos 100 times in: 0.00010705/sec concat 100 times in: 0.00021315/sec end/key 100 times in: 0.00018001/sec count-- 100 times in: 0.00017095/sec
Output for 7.1.5
strpos 100 times in: 0.00010896/sec concat 100 times in: 0.00020099/sec end/key 100 times in: 0.00015903/sec count-- 100 times in: 0.00015998/sec
Output for 7.1.4
strpos 100 times in: 0.00011683/sec concat 100 times in: 0.00016713/sec end/key 100 times in: 0.00016999/sec count-- 100 times in: 0.00018382/sec
Output for 7.1.3
strpos 100 times in: 0.00018120/sec concat 100 times in: 0.00030494/sec end/key 100 times in: 0.00027084/sec count-- 100 times in: 0.00028205/sec
Output for 7.1.2
strpos 100 times in: 0.00010300/sec concat 100 times in: 0.00024199/sec end/key 100 times in: 0.00018406/sec count-- 100 times in: 0.00019002/sec
Output for 7.1.1
strpos 100 times in: 0.00010204/sec concat 100 times in: 0.00017500/sec end/key 100 times in: 0.00016499/sec count-- 100 times in: 0.00016499/sec
Output for 7.1.0
strpos 100 times in: 0.00012684/sec concat 100 times in: 0.00024986/sec end/key 100 times in: 0.00023794/sec count-- 100 times in: 0.00019813/sec
Output for 5.6.38
strpos 100 times in: 0.00043607/sec concat 100 times in: 0.00044894/sec end/key 100 times in: 0.00037289/sec count-- 100 times in: 0.00036097/sec

preferences:
63.8 ms | 467 KiB | 5 Q