Skip to content

Instantly share code, notes, and snippets.

@qiangxue
Created July 29, 2014 22:20
Show Gist options
  • Save qiangxue/1a4a8befc968fa7399f8 to your computer and use it in GitHub Desktop.
Save qiangxue/1a4a8befc968fa7399f8 to your computer and use it in GitHub Desktop.
<?php
/**
strncmp : 0.099173069000244
substr : 0.11933302879333
substr_compare : 0.10576391220093
*/
$str = 'namespace yii\web';
$str2 = 'namespace';
$n = 100000;
$time = microtime(true);
for ($i=0; $i<$n; ++$i) {
strncmp($str, $str2, 9);
}
echo "Total : " . (microtime(true) - $time) . "\n";
$time = microtime(true);
for ($i=0; $i<$n; ++$i) {
substr($str, 0, 9) === $str2;
}
echo "Total : " . (microtime(true) - $time) . "\n";
$time = microtime(true);
for ($i=0; $i<$n; ++$i) {
substr_compare($str, $str2, 0, 9);
}
echo "Total : " . (microtime(true) - $time) . "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment