3v4l.org

run code in 300+ PHP versions simultaneously
<?php function a($string, $length) { $string = substr($string, 0, $length); $string = substr($string, 0, strrpos($string, ' ')); return $string; } function b($string, $length) { $words = explode(' ', $string); $string = array_reduce($words, function ($current, $item) use ($length) { return mb_strlen($current) > $length ? $current : "$current$item "; }, ''); return $string; } $length = 40; $string = 'The fox flew over the moon. The cow did a big twist into the pool.'; var_dump(a($string, $length)); var_dump(b($string, $length));

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.10.0100.00723.57
8.3.140.0110.00720.82
8.3.130.0100.01018.37
8.3.120.0100.00018.83
8.3.110.0070.00320.94
8.3.100.0100.00724.06
8.3.90.0050.00326.77
8.3.80.0030.00817.97
8.3.70.0090.00918.30
8.3.60.0110.01118.43
8.3.50.0140.00416.72
8.3.40.0030.01023.70
8.3.30.0120.00318.41
8.3.20.0040.00424.18
8.3.10.0040.00424.66
8.3.00.0070.00026.16
8.2.260.0000.00917.40
8.2.250.0170.00316.62
8.2.240.0030.00618.91
8.2.230.0030.00622.58
8.2.220.0190.00037.54
8.2.210.0050.00226.77
8.2.200.0040.00418.88
8.2.190.0100.01018.04
8.2.180.0150.00616.32
8.2.170.0110.01119.02
8.2.160.0030.01722.96
8.2.150.0040.00425.66
8.2.140.0150.00324.66
8.2.130.0040.00426.16
8.2.120.0070.00026.16
8.2.110.0030.00620.47
8.2.100.0080.00417.84
8.2.90.0040.00417.63
8.2.80.0040.00418.78
8.2.70.0040.00417.63
8.2.60.0040.00417.50
8.2.50.0040.00417.63
8.2.40.0030.00519.48
8.2.30.0030.00617.53
8.2.20.0000.00917.54
8.2.10.0090.00917.69
8.2.00.0050.01117.68
8.1.310.0030.00616.54
8.1.300.0070.01019.59
8.1.290.0040.00430.84
8.1.280.0040.01125.92
8.1.270.0080.00024.66
8.1.260.0080.00026.35
8.1.250.0080.00028.09
8.1.240.0050.00522.11
8.1.230.0080.00420.90
8.1.220.0000.00818.60
8.1.210.0030.00518.77
8.1.200.0050.00317.35
8.1.190.0050.00317.36
8.1.180.0000.00922.08
8.1.170.0070.00017.62
8.1.160.0040.00717.40
8.1.150.0100.00717.27
8.1.140.0170.00017.29
8.1.130.0070.00917.30
8.1.120.0000.01617.39
8.1.110.0110.00617.34
8.1.100.0120.00417.48
8.1.90.0120.00417.50
8.1.80.0100.00517.36
8.1.70.0130.00417.44
8.1.60.0170.00317.55
8.1.50.0140.00717.58
8.1.40.0150.00317.68
8.1.30.0150.00017.61
8.1.20.0150.00517.70
8.1.10.0140.00517.61
8.1.00.0150.00417.51
8.0.300.0000.00718.77
8.0.290.0120.00416.58
8.0.280.0090.00316.73
8.0.270.0130.00316.74
8.0.260.0080.00816.81
8.0.250.0080.00816.81
8.0.240.0120.00416.82
8.0.230.0050.00916.82
8.0.220.0130.00016.80
8.0.210.0070.00716.73
8.0.200.0070.00516.82
8.0.190.0090.00516.86
8.0.180.0090.00216.75
8.0.170.0110.00416.86
8.0.160.0040.00916.77
8.0.150.0080.00816.73
8.0.140.0080.00816.85
8.0.130.0120.00416.76
8.0.120.0100.00516.79
8.0.110.0150.00016.73
8.0.100.0110.00616.73
8.0.90.0140.00216.88
8.0.80.0100.00516.79
8.0.70.0060.01016.73
8.0.60.0130.00216.86
8.0.50.0140.00016.83
8.0.30.0120.00416.84
8.0.20.0050.01016.81
8.0.10.0100.00516.73

preferences:
58.22 ms | 403 KiB | 5 Q