3v4l.org

run code in 300+ PHP versions simultaneously
<?php function benchmark(string $name, callable $callback) { $string = ')(§"!$&&))§/$"=$/%$%$!§%%"§=)$(&§"&$/%!(%(!$(%$!=!%==!§(%§$8234@#$ABCFG3478&* hello'; $time = microtime(true); for ($i = 0; $i < 100_000; ++$i) { $callback($string); } $time = microtime(true) - $time; echo number_format($time, 6), 's: ', $name, ' (result: ', $callback($string), ")\n"; } benchmark('preg', function (string $string) { $firstLetter = null; if (preg_match('/[a-z0-9]/i', $string, $match)) { $firstLetter = $match[0]; } return $firstLetter; }); benchmark('ctype_alnum', function (string $string) { $length = strlen($string); $firstLetter = null; for ($i = 0; $i < $length; ++$i) { if (ctype_alnum($string[$i])) { $firstLetter = $string[$i]; break; } } return $firstLetter; }); benchmark('strcspn', function (string $string) { return $string[strcspn($string, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")] ?? null; });
Output for 7.4.8
0.033587s: preg (result: 8) 0.225340s: ctype_alnum (result: 8) 0.694287s: strcspn (result: 8)
Output for 7.4.7
0.062920s: preg (result: 8) 0.259254s: ctype_alnum (result: 8) 0.637390s: strcspn (result: 8)
Output for 7.4.6
0.040168s: preg (result: 8) 0.240447s: ctype_alnum (result: 8) 0.638092s: strcspn (result: 8)
Output for 7.4.5
0.064419s: preg (result: 8) 0.263502s: ctype_alnum (result: 8) 0.785059s: strcspn (result: 8)
Output for 7.4.4
0.041186s: preg (result: 8) 0.221145s: ctype_alnum (result: 8) 0.598469s: strcspn (result: 8)
Output for 7.4.3
0.037540s: preg (result: 8) 0.220236s: ctype_alnum (result: 8) 0.675878s: strcspn (result: 8)
Output for 7.4.2
0.041142s: preg (result: 8) 0.260334s: ctype_alnum (result: 8) 0.667934s: strcspn (result: 8)
Output for 7.4.1
0.059451s: preg (result: 8) 0.257777s: ctype_alnum (result: 8) 0.587912s: strcspn (result: 8)
Output for 7.4.0
0.052054s: preg (result: 8) 0.295840s: ctype_alnum (result: 8) 0.616256s: strcspn (result: 8)
Output for 7.2.0 - 7.2.32, 7.3.0 - 7.3.20
Parse error: syntax error, unexpected '_000' (T_STRING), expecting ';' in /in/EFdLW on line 8
Process exited with code 255.

preferences:
45.63 ms | 739 KiB | 4 Q