note 109277 added to function.strncmp
| From: | mansisce at gmail dot com | Date: | Mon, 02 Jul 2012 05:32:49 +0000 |
| Subject: | note 109277 added to function.strncmp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-188997@lists.php.net to get a copy of this message | ||
In the first 'user contributed notes' , if user would have given the o/p of his program
then it would have been great . Can user mention the output of his program ?
Ref :
For checking matches at the beginning of a short string, strpos() is about 15% faster than
strncmp().
Here's a benchmark program to prove it:
<?php
$haystack = "abcdefghijklmnopqrstuvwxyz";
$needles = array('abc', 'xyz', '123');
foreach ($needles as $needle) {
$times['strncmp'][$needle] = -microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$result = strncmp($haystack, $needle, 3) === 0;
}
$times['strncmp'][$needle] += microtime(true);
}
foreach ($needles as $needle) {
$times['strpos'][$needle] = -microtime(true);
for ($i = 0; $i < 1000000; $i++) {
$result = strpos($haystack, $needle) === 0;
}
$times['strpos'][$needle] += microtime(true);
}
var_export($times);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 115.111.20.34
----
Manual Page -- http://www.php.net/manual/en/function.strncmp.php
Edit -- https://master.php.net/note/edit/109277
Del: integrated -- https://master.php.net/note/delete/109277/integrated
Del: useless -- https://master.php.net/note/delete/109277/useless
Del: bad code -- https://master.php.net/note/delete/109277/bad+code
Del: spam -- https://master.php.net/note/delete/109277/spam
Del: non-english -- https://master.php.net/note/delete/109277/non-english
Del: in docs -- https://master.php.net/note/delete/109277/in+docs
Del: other reasons-- https://master.php.net/note/delete/109277
Reject -- https://master.php.net/note/reject/109277
Search -- https://master.php.net/manage/user-notes.php