note 72198 deleted from language.operators.comparison by danbrown

From: Date: Thu, 18 Jun 2009 14:06:38 +0000
Subject: note 72198 deleted from language.operators.comparison by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-157071@lists.php.net to get a copy of this message
Note Submitter: Stephen ---- 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...).

« previous php.notes (#157071) next »