note 80603 added to function.strncmp
| From: | codeguru at crazyprogrammer dot cba dot pl | Date: | Thu, 24 Jan 2008 19:07:50 +0000 |
| Subject: | note 80603 added to function.strncmp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135382@lists.php.net to get a copy of this message | ||
I ran the following experiment to compare arrays.
1 st - using (substr($key,0,5 == "HTTP_") & 2 nd - using (!strncmp($key,
'HTTP_', 5))
I wanted to work out the fastest way to get the first few characters from a array
BENCHMARK ITERATION RESULT IS:
if (substr($key,0,5 == "HTTP_").... - 0,000481s
if (!strncmp($key, 'HTTP_', 5)).... - 0,000405s
strncmp() is 20% faster than substr() :D
<?php
// SAMPLE FUNCTION
function strncmp_match($arr)
{
foreach ($arr as $key => $val)
{
//if (substr($key,0,5 == "HTTP_")
if (!strncmp($key, 'HTTP_', 5))
{
$out[$key] = $val;
}
}
return $out;
}
// EXAMPLE USE
?><pre><?php
print_r(strncmp_match($_SERVER));
?></pre>
will display code like this:
Array
(
[HTTP_ACCEPT] => XXX
[HTTP_ACCEPT_LANGUAGE] => pl
[HTTP_UA_CPU] => x64
[HTTP_ACCEPT_ENCODING] => gzip, deflate
[HTTP_USER_AGENT] => Mozilla/4.0
(compatible; MSIE 7.0;
Windows NT 5.1;
.NET CLR 1.1.4322;
.NET CLR 2.0.50727)
[HTTP_HOST] => XXX.XXX.XXX.XXX
[HTTP_CONNECTION] => Keep-Alive
[HTTP_COOKIE] => __utma=XX;__utmz=XX.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
)
----
Server IP: 82.160.94.10
Probable Submitter: 81.210.63.146
----
Manual Page -- http://www.php.net/manual/en/function.strncmp.php
Edit -- https://master.php.net/note/edit/80603
Del: integrated -- https://master.php.net/note/delete/80603/integrated
Del: useless -- https://master.php.net/note/delete/80603/useless
Del: bad code -- https://master.php.net/note/delete/80603/bad+code
Del: spam -- https://master.php.net/note/delete/80603/spam
Del: non-english -- https://master.php.net/note/delete/80603/non-english
Del: in docs -- https://master.php.net/note/delete/80603/in+docs
Del: other reasons-- https://master.php.net/note/delete/80603
Reject -- https://master.php.net/note/reject/80603
Search -- https://master.php.net/manage/user-notes.php