- strpos: documentation ( source)
- microtime: documentation ( source)
- str_repeat: documentation ( source)
- strncmp: documentation ( source)
- sprintf: documentation ( source)
<?php
$str = str_repeat('abc', 1000);
echo '$str = ', $str, "\n";
function test($name, $test) {
$t = microtime(true);
for ($i = 0; $i < 100000; ++$i) {
$test();
}
echo sprintf('%.6f', (microtime(true) - $t)), 's ', $name, "\n";
}
foreach (['abc', 'abb', 'bbc', str_repeat('b', 100)] as $prefix) {
echo "\n", '$prefix = ', $prefix, "\n";
test('strpos', function () use ($str, $prefix) {
return 0 === strpos($str, $prefix);
});
test('strncmp', function () use ($str, $prefix) {
return 0 === strncmp($str, $prefix, strlen($prefix));
});
test('[0] + strncmp', function () use ($str, $prefix) {
return $prefix[0] === $str[0] && 0 === strncmp($str, $prefix, strlen($prefix));
});
}
This script was stopped while abusing our resources