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 ...