- microtime: documentation ( source)
- preg_match: documentation ( source)
- preg_grep: documentation ( source)
- range: documentation ( source)
<?php
$array = range(0, 99);
$start = microtime(1);
$result = [];
for ($i = 0; $i < 100; $i++)
if (preg_match("#$i#", $array[$i]))
$result[] = $array[$i];
print "for \$i took ".($start - microtime(1))." seconds\n";
$start = microtime(1);
$result = [];
for ($i = 0; $i < 100; $i++)
if (preg_match("#\d#", $array[$i]))
$result[] = $array[$i];
print "for \\d took ".($start - microtime(1))." seconds\n";
$start = microtime(1);
$result = [];
foreach ($array as $entry)
if (preg_match("#\d#", $entry))
$result[] = $entry;
print "foreach \\d took ".($start - microtime(1))." seconds\n";
$start = microtime(1);
$result = [];
$result = preg_grep("#\d#", $array);
print "preg_grep \\d took ".($start - microtime(1))." seconds\n";