{"id":392,"date":"2016-03-04T21:55:07","date_gmt":"2016-03-04T21:55:07","guid":{"rendered":"http:\/\/blog.trungson.com\/?p=392"},"modified":"2016-03-04T21:55:07","modified_gmt":"2016-03-04T21:55:07","slug":"optimization-in_array","status":"publish","type":"post","link":"http:\/\/blog.trungson.com\/?p=392","title":{"rendered":"Optimization with in_array()"},"content":{"rendered":"<p>Ever wonder with many in_array() checks, you can speed it up depending on your scenario. In my case, 99% it&#8217;s empty so it&#8217;s better with an empty() check<\/p>\n<p>&lt;?php<br \/>\n$vResult = array();<br \/>\n$vAry = array();<br \/>\n$vLimit = 10000000;<\/p>\n<p>$vStart = microtime(true);<br \/>\nfor ($i=1;$i&lt;$vLimit; $i++) {<br \/>\nif (in_array(123,$vAry)) {<br \/>\n}<br \/>\n} \/\/ rof<br \/>\n$vResult[] = &#8220;NoEmptyCheck= &#8220;.(microtime(true)-$vStart);<\/p>\n<p>$vStart = microtime(true);<br \/>\nfor ($i=1;$i&lt;$vLimit; $i++) {<br \/>\nif (!empty($vAry) &amp;&amp; in_array(123,$vAry)) {<br \/>\n}<br \/>\n} \/\/ rof<br \/>\n$vResult[] = &#8220;WithEmptyCheck= &#8220;.(microtime(true)-$vStart);<\/p>\n<p>echo implode(&#8220;\\n&#8221;,$vResult).&#8221;\\n&#8221;;<\/p>\n<p>\/* Result<\/p>\n<p>For an often-non-empty array scenario:<br \/>\n&#8211; NoEmptyCheck= 3.76290512085<br \/>\n&#8211; WithEmptyCheck= 4.59545087814<\/p>\n<p>For an often-empty array scenario:<br \/>\n&#8211; NoEmptyCheck= 3.46415710449<br \/>\n&#8211; WithEmptyCheck= 1.08467411995<\/p>\n<p>*\/<\/p>\n<p>?&gt;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wonder with many in_array() checks, you can speed it up depending on your scenario. In my case, 99% it&#8217;s empty so it&#8217;s better with an empty() check &lt;?php $vResult = array(); $vAry = array(); $vLimit = 10000000; $vStart = microtime(true); for ($i=1;$i&lt;$vLimit; $i++) { if (in_array(123,$vAry)) { } } \/\/ rof $vResult[] = &#8220;NoEmptyCheck= [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/392"}],"collection":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=392"}],"version-history":[{"count":2,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/392\/revisions"}],"predecessor-version":[{"id":394,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=\/wp\/v2\/posts\/392\/revisions\/394"}],"wp:attachment":[{"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=392"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=392"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.trungson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=392"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}