note 72198 added to language.operators.comparison
| From: | Stephen at osu1 dot php dot net | Date: | Sun, 07 Jan 2007 13:12:25 +0000 |
| Subject: | note 72198 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121378@lists.php.net to get a copy of this message | ||
Regarding gamephreak AT removethis gmail DOT com,
I found those results interesting, so I decided to test them on my own dedicated server. I found
that actually, the identity and equality operators are almost exactly the same speed when dealing
with numbers and/or strings (I did not test arrays, objects, etc). I ran the same tests plus a
couple of inequality tests as well, this time over a 100,000 iteration loop (rounded to 7
significant digits using round()). :
'foo' == 'foo': 0.0130451
'foo' === 'foo': 0.0124481
42 == 42: 0.0124421
42 === 42: 0.0124369
'foo' == 'bar': 0.0124562
'foo' === 'bar': 0.012475
42 == 43: 0.012471
42 === 43: 0.0124719
'42' == 42: 0.0124779
'42' === 42: 0.0124731
'42' == 43: 0.0124722
'42' === 43: 0.01248
42 != 43: 0.012444
42 !== 43: 0.0124211
This was just one snapshot, though. If I refreshed the page I was using to test this, sometimes the
"winners" would reverse. In other words, no operator was clearly faster, and I think that
system "noise" is may usually be more significant than the speed of these operators.
Here is sample code for one of the loops:
$time1 = microtime(true);
for ($i = 0; $i < 100000; $i++){
'42' == 43;
}
$time2 = microtime(true);
print "'42' == 43: " . round($time2 - $time1, 7) .
'<br><br>';
The lesson here is that you should make your coding decisions based on what makes more sense from a
logical perspective, and not what is faster. Personally I hate the equality operator since
PHP's dynamic typing makes it unpredictable (e.g. two strings compared may both be cast as
numbers...).
----
Server IP: 66.163.161.117
Probable Submitter: 68.163.130.212
----
Manual Page -- http://www.php.net/manual/en/language.operators.comparison.php
Edit -- https://master.php.net/note/edit/72198
Del: integrated -- https://master.php.net/note/delete/72198/integrated
Del: useless -- https://master.php.net/note/delete/72198/useless
Del: bad code -- https://master.php.net/note/delete/72198/bad+code
Del: spam -- https://master.php.net/note/delete/72198/spam
Del: non-english -- https://master.php.net/note/delete/72198/non-english
Del: in docs -- https://master.php.net/note/delete/72198/in+docs
Del: other reasons-- https://master.php.net/note/delete/72198
Reject -- https://master.php.net/note/reject/72198
Search -- https://master.php.net/manage/user-notes.php