Why is array_key_exists 1000x slower than isset on referenced arrays?

后端 未结 3 2081
轻奢々
轻奢々 2020-12-31 00:56

I have found that array_key_exists is over 1000x slower than isset at check if a key is set in an array reference. Does anyone that has an understa

3条回答
  •  长发绾君心
    2020-12-31 02:00

    Not array_key_exists, but the removal of the reference (= NULL) causes this. I commented it out from your script and this is the result:

    array_key_exists( $my_array ) 0.0059430599212646
    isset( $my_array ) 0.0027170181274414
    array_key_exists( $my_array_ref ) 0.0038740634918213
    isset( $my_array_ref ) 0.0025200843811035
    

    Only removed the unsetting from the array_key_exists( $my_array_ref ) part, this is the modified part for reference:

    $my_array = array();
    $my_array_ref = &$my_array;
    $start = microtime( TRUE );
    for( $i = 1; $i < 10000; $i++ ) {
        array_key_exists( $i, $my_array_ref );
        // $my_array_ref[$i] = NULL;
    }
    $stop = microtime( TRUE );
    print "array_key_exists( \$my_array_ref ) ".($stop-$start).PHP_EOL;
    unset( $my_array, $my_array_ref, $start, $stop, $i );
    

提交回复
热议问题