Use In_array () foreach Array_search () in PHP to find the performance comparison when an array is included _php

Source: Internet
Author: User
Keywords In_array foreach array_search

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.

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
There are many ways to determine whether a character is included in an array. Newbies who have just learned php tend to use loops to solve this problem. For small websites in general, this solution will not cause any major problems. But in terms of performance, this method is not the best method. Below I will use the three methods of foreach, in_array () array_search to compare the performance differences of these three methods.




___FCKpd___0


The execution time of the above program is as follows:


in_array ()



foreach



array_search ()



From the above, we can roughly see the performance of these three methods. The performance of array_search and in_array is similar, and the performance of foreach is the worst.


b;
echo "Execution time:". $ runtime-> spent (). "ms";

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) * 1000, 1);
  }
}
?>
The execution time of the above program is as follows:

in_array ()

foreach

array_search ()

From the above, we can roughly see the performance of these three methods. The performance of array_search and in_array is similar, and the performance of foreach is the worst.

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.