Use in_array () foreacharray_search () in php to find whether the array contains Performance Comparison. phpforeach two-dimensional array _ PHP Tutorial

Source: Internet
Author: User
In php, use in_array () foreacharray_search () to check whether the array contains the Performance Comparison. phpforeach two-dimensional array. Php uses in_array () foreacharray_search () to check whether an array contains the same performance. phpforeach uses a two-dimensional array to determine whether a character is included in an array, I just used in_array () foreach array_search () in php to find whether the array contains performance comparison, phpforeach two-dimensional array

There are many methods to determine whether a character is included in an array. new users who are just learning php tend to use loops. for small websites, this solution will not cause any major problems. However, in terms of performance, this method is not the best method. I will compare the performance differences between the three methods, foreach and in_array () array_search.

<? 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-> sp Ent (). "millisecond"; 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 programs is shown in:

In_array ()

Foreach

Array_search ()

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

Comparison () foreach array_search () compares the performance of array inclusion. phpforeach two-dimensional array determines whether a character is included in an array. There are many 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.