RE: [PHP] string comparison
| From: | John Holmes | Date: | Sat, 27 Jul 2002 16:47:27 +0000 |
| Subject: | RE: [PHP] string comparison | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-109985@lists.php.net to get a copy of this message | ||
Strcmp() is case sensitive. So use it when you need a case sensitive
comparison.
---John Holmes...
> -----Original Message-----
> From: Bas Jobsen [mailto:bas@startpunt.cc]
> Sent: Saturday, July 27, 2002 12:35 PM
> To: PHP General
> Subject: [PHP] string comparison
>
> Hello,
>
> When should i prefer strcmp() above == or !=?
> I have test it out (see code below).
> But a == comparison seems to be faster always.
> First i thought is was cause, if(strcmp($1,$2)!=0),
> needs two comparisons (strcmp and !=). But even
> if($1!=$2) is faster then if(strcmp($1,$2)).
>
> Thanks,
>
> Bas Jobsen
>
> <?
>
> function getmicrotime(){
> list($usec, $sec) = explode(" ",microtime());
> return ((float)$usec + (float)$sec);
> }
>
>
> $t1='hello';
> $t2='hello';
> $t3='hallo';
>
> ?>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if(strcmp($t1,$t3));//nonmatch
> echo getmicrotime()-$start;
> ?>
> <br>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if($t1!=$t3);//nonmatch
> echo getmicrotime()-$start;
> ?>
>
> <br>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if(!strcmp($t1,$t3));//match
> echo getmicrotime()-$start;
> ?>
> <br>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if($t1==$t3);//match
> echo getmicrotime()-$start;
> ?>
> <br>
>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if(strcmp($t1,$t2));//nonmatch
> echo getmicrotime()-$start;
> ?>
> <br>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if($t1!=$t2);//nonmatch
> echo getmicrotime()-$start;
> ?>
>
> <br>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if(!strcmp($t1,$t2));//match
> echo getmicrotime()-$start;
> ?>
> <br>
> 1000x:<?
> $start=getmicrotime();
> for($a=0; $a<100000; $a++) if($t1==$t2);//match
> echo getmicrotime()-$start;
> ?>
> <br>
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php