Use In_array () foreach Array_search () in PHP to find performance comparisons when arrays are included, Phpforeach two-dimensional arrays _php tutorial

Source: Internet
Author: User

PHP uses In_array () foreach Array_search () to find out if an array contains performance comparisons, Phpforeach two-dimensional arrays


To determine whether a character is contained in an array or not, there are many ways that beginners who have just learned PHP tend to use loops to solve the problem, and for a small web site, this solution is not going to be a big deal. But in terms of performance, this method is not the best method, the following author of Foreach,in_array () Array_search These three methods to compare the performance of these three methods of differences.

<?php$runtime= new Runtime; $runtime->start ();    $a = ' k ';    $b = Array (' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' I ', ' j ', ' K ');/*for ($i =0; $i < 100000; $i + +) {    var_dump (In_array ($a, $b));    } *//*for ($i =0; $i < 100000; $i + +) {    foreach ($b as $key + $value) {        if ($a = = $value) {            //echo True;
  continue;}}}    *//*for ($i =0; $i < 100000; $i + +) {    array_search ($a, $b);} */$runtime->stop (); Echo $_b;echo "Execution time:" $runtime->spent (). "Milliseconds"; class runtime{  var $StartTime = 0;  var $StopTime = 0;  function Get_microtime () {    list ($usec, $sec) = explode (' ', Microtime ());    return (float) $usec + (float) $sec);  }  function Start () {    $this->starttime = $this->get_microtime ();  }  function Stop () {    $this->stoptime = $this->get_microtime ();  }  function spent () {    return round (($this->stoptime-$this->starttime) * (1);  }}? >

The above program execution time is as follows:

In_array ()

Foreach

Array_search ()

It can be seen from the above that the performance of these three methods, Array_search and In_array performance is similar, foreach performance is the worst.

http://www.bkjia.com/PHPjc/984627.html www.bkjia.com true http://www.bkjia.com/PHPjc/984627.html techarticle PHP uses In_array () foreach Array_search () to find out whether an array contains performance comparisons, Phpforeach two-dimensional arrays to determine whether a character is contained in an array, a number of methods, just ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.