Forum Moderators: coopster

Message Too Old, No Replies

strcmp or ===?

What's faster?

         

Warboss Alex

3:32 pm on May 3, 2004 (gmt 0)

10+ Year Member



Hello, all.

For comparisons between strings, what's faster, the strcmp() function or the manual way (if ($x === $y) .. ) ..?

I've heard mostly that strcmp() is faster, just wanted confirmation from the experts here.. ;) ..

coopster

5:23 pm on May 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Not in my tests. strcmp is consistently slower, but not by much, about a tenth of a second -- except when the comparison values are numeric, then strcmp is consistently 3 times slower (probably because it has to convert the values to string representation first).

DrDoc

5:49 pm on May 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



=== is usually faster
The reason for this being the fact that there are two checks:
1) Are the varibles of the same type? If so, fail immediately
2) Do the variables have the exact same values?

strcmp() first converts the variables to strings, then compares them.

Therefore, the two can be explained thus:

strcmp():
strval($x) == strval($y)

===:
gettype($x) == gettype($y) && $x == $y