3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* span :: (a -> Bool) -> [a] -> ([a],[a]) span _ xs@[] = (xs, xs) span p xs@(x:xs') | p x = let (ys,zs) = span p xs' in (x:ys,zs) | otherwise = ([],xs) */ function span($p, $xs) { if (empty($xs)) { return [[], []]; } $xs_ = $xs; $x = array_shift($xs_); if ($p($x)) { list($ys, $zs) = span($p, $xs_); return [array_merge([$x], $ys), $zs]; } return [[], $xs]; } /* groupBy :: (a -> a -> Bool) -> [a] -> [[a]] groupBy _ [] = [] groupBy eq (x:xs) = (x:ys) : groupBy eq zs where (ys,zs) = span (eq x) xs */ function array_group(callable $eq, array $xs) { if (empty($xs)) { return []; } $x = array_shift($xs); // curry of $eq $eqx = function ($y) use ($eq, $x) { return $eq($x, $y); }; list($ys, $zs) = span($eqx, $xs); return array_merge([array_merge([$x], $ys)], array_group($eq, $zs)); } function it($m,$p){echo ($p?'OK':'NO')." It $m\n"; if(!$p){$GLOBALS['f']=1;}} function done(){if(@$GLOBALS['f'])die(1);} it('spans elements which match', span(function ($x) { return $x < 3; }, [1, 2, 3]) === [[1, 2], [3]]); it('spans elements which match from the start', span(function ($x) { return $x == 3; }, [1, 6, 3, 3]) === [[], [1, 2, 3, 3]]); it('spans returns empty lists on empty list', span(function ($x) { return false; }, []) === [[], []]); it('groups elements recursively', array_group(function ($a, $b) { return $a <= ($b-1); }, [1, 1, 2, 1]) === [[1, 1], [2], [1]]); print_r(array_group(function ($a, $b) { return $a <= ($b-1); }, [1, 1, 2, 1]) === [[1, 1], [2], [1]]));

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.340.0080.03412.05
5.4.320.0040.03912.54
5.4.310.0100.03812.55
5.4.300.0100.04712.55
5.4.290.0110.04812.54
5.4.280.0050.03712.44
5.4.270.0030.04012.44
5.4.260.0060.04012.44
5.4.250.0070.04112.44
5.4.240.0060.03612.44
5.4.230.0050.03912.43
5.4.220.0050.03612.43
5.4.210.0050.03712.43
5.4.200.0080.03712.43
5.4.190.0070.03512.43
5.4.180.0110.03412.43
5.4.170.0060.03912.44
5.4.160.0110.05112.43
5.4.150.0090.03912.43
5.4.140.0080.04912.12
5.4.130.0070.03812.10
5.4.120.0070.03512.07
5.4.110.0040.03812.06
5.4.100.0070.03612.06
5.4.90.0050.05512.06
5.4.80.0080.03412.06
5.4.70.0050.03612.06
5.4.60.0050.04512.06
5.4.50.0050.03812.06
5.4.40.0060.03512.04
5.4.30.0070.03312.05
5.4.20.0050.04412.04
5.4.10.0060.03712.04
5.4.00.0060.03811.53
5.3.290.0060.04212.80
5.3.280.0050.03912.71
5.3.270.0080.03912.72
5.3.260.0100.04412.72
5.3.250.0120.03312.72
5.3.240.0100.04912.71
5.3.230.0050.03912.71
5.3.220.0060.04012.68
5.3.210.0080.03812.68
5.3.200.0040.03812.68
5.3.190.0050.03812.67
5.3.180.0070.03512.67
5.3.170.0060.04012.67
5.3.160.0040.04312.67
5.3.150.0060.03912.68
5.3.140.0060.03712.66
5.3.130.0040.04112.66
5.3.120.0050.04412.66
5.3.110.0050.04312.66
5.3.100.0050.03712.13
5.3.90.0070.03512.11
5.3.80.0040.03812.09
5.3.70.0090.03412.09
5.3.60.0060.03812.09
5.3.50.0040.03812.02
5.3.40.0070.04012.02
5.3.30.0040.03811.98
5.3.20.0060.03711.76
5.3.10.0040.03711.74
5.3.00.0070.03511.71
5.2.170.0030.0329.22
5.2.160.0050.0289.22
5.2.150.0040.0309.22
5.2.140.0070.0339.22
5.2.130.0050.0309.18
5.2.120.0050.0349.18
5.2.110.0060.0309.18
5.2.100.0080.0289.18
5.2.90.0080.0279.17
5.2.80.0060.0289.17
5.2.70.0060.0289.17
5.2.60.0040.0319.13
5.2.50.0040.0339.10
5.2.40.0030.0329.07
5.2.30.0040.0329.05
5.2.20.0070.0319.04
5.2.10.0070.0298.95
5.2.00.0060.0278.81
5.1.60.0050.0238.09
5.1.50.0030.0258.09
5.1.40.0050.0248.06
5.1.30.0030.0278.42
5.1.20.0080.0258.43
5.1.10.0040.0268.17
5.1.00.0050.0278.16
5.0.50.0070.0196.64
5.0.40.0040.0206.50
5.0.30.0020.0336.31
5.0.20.0050.0236.29
5.0.10.0040.0206.26
5.0.00.0050.0296.25
4.4.90.0030.0164.77
4.4.80.0040.0154.75
4.4.70.0020.0174.75
4.4.60.0030.0154.75
4.4.50.0010.0174.77
4.4.40.0020.0304.71
4.4.30.0040.0204.76
4.4.20.0020.0234.85
4.4.10.0040.0174.85
4.4.00.0020.0254.76
4.3.110.0020.0174.67
4.3.100.0030.0144.66
4.3.90.0040.0134.63
4.3.80.0030.0244.59
4.3.70.0050.0184.63
4.3.60.0020.0164.62
4.3.50.0010.0174.63
4.3.40.0040.0234.54
4.3.30.0010.0173.30
4.3.20.0000.0183.27
4.3.10.0030.0143.24
4.3.00.0130.0236.86

preferences:
133.13 ms | 1394 KiB | 7 Q