3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ 'car_porsche', 'caravan', 'car', 'motorcycle_suzuki', 'motorcycle_carabela', ]; $search = 'car'; function keepIfStartsWith_regex(array $haystack, string $needle): array { return preg_grep('/^' . preg_quote($needle, '/') . '/', $haystack); } function removeIfStartsWith_regex(array $haystack, string $needle): array { return preg_grep('/^' . preg_quote($needle, '/') . '/', $haystack, PREG_GREP_INVERT); } function keepIfStartsWith_PHP8(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => str_starts_with($v, $needle)); } function removeIfStartsWith_PHP8(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => !str_starts_with($v, $needle)); } function keepIfStartsWith_PHP7_4(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => strpos($v, $needle) === 0); } function removeIfStartsWith_PHP7_4(array $haystack, string $needle): array { return array_filter($haystack, fn($v) => strpos($v, $needle) !== 0); } function keepIfStartsWith_sub7_4(array $haystack, string $needle): array { return array_filter($haystack, function($v) use($needle) { return strpos($v, $needle) === 0; }); } function removeIfStartsWith_sub7_4(array $haystack, string $needle): array { return array_filter($haystack, function($v) use($needle) { return strpos($v, $needle) !== 0; }); } $functions = [ 'keepIfStartsWith_regex', 'removeIfStartsWith_regex', 'keepIfStartsWith_PHP8', 'removeIfStartsWith_PHP8', 'keepIfStartsWith_PHP7_4', 'removeIfStartsWith_PHP7_4', 'keepIfStartsWith_sub7_4', 'removeIfStartsWith_sub7_4', ]; foreach ($functions as $fn) { echo "$fn: " . json_encode(array_values($fn($array, $search))) . "\n"; }

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)
8.4.120.0100.01020.66
8.4.110.0070.00822.70
8.4.100.0060.00218.75
8.4.90.0120.00918.03
8.4.80.0130.00718.07
8.4.70.0110.00717.90
8.4.60.0080.00918.60
8.4.50.0060.00418.93
8.4.40.0100.01020.54
8.4.30.0130.00318.70
8.4.20.0050.00319.65
8.4.10.0110.00724.23
8.3.250.0150.00418.81
8.3.240.0120.00616.55
8.3.230.0140.00616.52
8.3.220.0080.00317.07
8.3.210.0060.00216.61
8.3.200.0110.00816.54
8.3.190.0120.00719.12
8.3.180.0040.00416.67
8.3.170.0090.00917.23
8.3.160.0090.00920.39
8.3.150.0150.00419.04
8.3.140.0030.00616.66
8.3.130.0060.00318.67
8.3.120.0060.00320.49
8.3.110.0080.00820.94
8.3.100.0140.00724.06
8.3.90.0040.00426.77
8.3.80.0110.00016.75
8.3.70.0070.01716.63
8.3.60.0120.00316.50
8.3.50.0000.01518.50
8.3.40.0070.00720.21
8.3.30.0120.00418.92
8.3.20.0080.00024.18
8.3.10.0050.00324.66
8.3.00.0040.00426.16
8.2.290.0050.00520.53
8.2.280.0130.00718.40
8.2.270.0140.00419.00
8.2.260.0080.00019.02
8.2.250.0040.01217.00
8.2.240.0000.01117.27
8.2.230.0060.00322.58
8.2.220.0070.00337.54
8.2.210.0050.00326.77
8.2.200.0060.00316.63
8.2.190.0140.01117.00
8.2.180.0070.01125.92
8.2.170.0140.00719.01
8.2.160.0060.01222.96
8.2.150.0080.00025.66
8.2.140.0080.00024.66
8.2.130.0040.00426.16
8.2.120.0000.00721.19
8.2.110.0030.00719.30
8.2.100.0040.00817.91
8.2.90.0040.00418.09
8.2.80.0080.00018.16
8.2.70.0040.00417.75
8.2.60.0040.00417.75
8.2.50.0000.00817.75
8.2.40.0030.00619.57
8.2.30.0030.00618.07
8.2.20.0000.00720.56
8.2.10.0040.00318.32
8.2.00.0040.00419.39
8.1.330.0110.00822.06
8.1.320.0120.00816.36
8.1.310.0000.00818.63
8.1.300.0080.00816.61
8.1.290.0100.00018.88
8.1.280.0150.00625.92
8.1.270.0080.00822.27
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0030.00719.32
8.1.230.0110.00020.98
8.1.220.0040.00417.74
8.1.210.0000.00819.09
8.1.200.0030.00617.61
8.1.190.0000.00917.48
8.1.180.0030.00618.10
8.1.170.0030.00517.62
8.1.160.0070.00018.82
8.1.150.0000.00718.88
8.1.140.0050.00517.57
8.1.130.0080.00017.63
8.1.120.0040.00417.64
8.1.110.0050.00317.65
8.1.100.0000.00817.75
8.1.90.0000.00917.69
8.1.80.0000.00817.60
8.1.70.0080.00017.51
8.1.60.0100.00017.80
8.1.50.0000.00717.67
8.1.40.0040.00417.72
8.1.30.0060.00317.73
8.1.20.0030.00517.74
8.1.10.0080.00017.76
8.1.00.0050.00217.62
8.0.300.0080.00020.28
8.0.290.0080.00017.00
8.0.280.0070.00018.51
8.0.270.0000.00717.00
8.0.260.0000.00717.37
8.0.250.0080.00017.37
8.0.240.0030.00317.37
8.0.230.0000.00817.37
8.0.220.0030.00317.37
8.0.210.0000.00717.37
8.0.200.0060.00317.37
8.0.190.0050.00317.37
8.0.180.0080.00017.37
8.0.170.0000.00817.37
8.0.160.0040.00417.37
8.0.150.0000.00917.37
8.0.140.0030.00518.75
8.0.130.0000.00717.37
8.0.120.0000.00918.75
8.0.110.0040.00417.37
8.0.100.0040.00417.37
8.0.90.0030.00317.37
8.0.80.0040.00417.37
8.0.70.0020.00517.37
8.0.60.0000.00717.37
8.0.50.0030.00617.37
8.0.30.0000.00718.66
8.0.20.0050.00318.75
8.0.10.0050.00318.79

preferences:
26.27 ms | 403 KiB | 5 Q