Test In_array, Isset, array_key_exists performance in PHP

Source: Internet
Author: User

Test In_array, Isset, array_key_exists performance. Self-written simple test code:

Ini_set(' Display_errors ',true);error_reporting(E_all);d Ate_default_timezone_set (' PRC ');functionTtt$a,$b){    $u 1=$b[0]-$a[0]; $u 2=$b[1]-$a[1]; Echo Number_format($u 1, 8). ' Seconds '.$u 2.‘ bytes; Echo' <br/> ';}$t 0= [    Microtime(true),memory_get_usage ()];$arr 1= [];$arr 2= []; for($i= 0;$i< 1000;$i++ ){     $arr 1[] = ' a '.$i; $arr 2[' A '.$i] =true;}$k= ' a862 ';$t 1= [    Microtime(true),memory_get_usage ()]; for($i= 0;$i< 100;$i++ ){     In_array($k,$arr 1);}$t 2= [    Microtime(true),memory_get_usage ()]; for($i= 0;$i< 100;$i++ ){     isset($arr 2[$k]);}$t 3= [    Microtime(true),memory_get_usage ()]; for($i= 0;$i< 100;$i++ ){     array_key_exists($k,$arr 2);}$t 4= [    Microtime(true),memory_get_usage ()];ttt ($t 1,$t 2); TTT ($t 2,$t 3); TTT ($t 3,$t 4);

Results from my Computer output:

0.01480007 sec 328 bytes
0.00003886 sec 328 bytes
0.00008416 sec 328 bytes

Rough it's best to use Isset, In_array consumes more time than array_key_exists.

If in the case of data volume comparison, the situation of these three is basically close, but still isset is the fastest.

Therefore, in the design of nilcms to consider this aspect of the problem. Remember.

Test In_array, Isset, array_key_exists performance in PHP

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.