zoukankan      html  css  js  c++  java
  • isset !empty array_key_exists 厉害了我的哥

    The other responses focus on the differences between the two functions. This is true, but if the source array does not contain null or 0 or "", ... (empty values) values you can benchmark the speed of the two functions:

    <?php
    
    function makeRandomArray( $length ) {
        $array = array();
        for ($i = 0; $i < $length; $i++) {
            $array[$i] = rand(1, $length);
        }
    
        return $array;
    }
    
    function benchmark( $count, $function ) {
        $start = microtime(true);
        for ($i = 0; $i < $count; $i++) {
            $function();
        }
        return microtime(true) - $start;
    }
    
    $runs = 100000;
    $smallLength = 10;
    $small = makeRandomArray($smallLength);
    
    var_dump(benchmark($runs, function() {
        global $small, $smallLength;
        array_key_exists(rand(0, $smallLength), $small);
    }));
    var_dump(benchmark($runs, function() {
        global $small, $smallLength;
        !empty($small[rand(0, $smallLength)]);
    }));

    Which gave me the following results:

    For a small array:

    • array_key_exists: float(0.18357992172241)
    • empty: float(0.072798013687134)
    • isset: float(0.070242881774902)

    For a relative big array:

    • array_key_exists: float(0.57489585876465)
    • empty: float(0.0068421363830566)
    • isset: float(0.0069410800933838)

    So if it's possible it's faster to use empty or isset.
    -----------
    https://stackoverflow.com/questions/6884609/array-key-existskey-array-vs-emptyarraykey

  • 相关阅读:
    git功能速查
    iPad actionsjeet
    iOS开发中集成Reveal
    【转】ios内联函数 inline
    【转】数据存储——APP 缓存数据线程安全问题探讨
    iOS 改变导航栏高度
    ios 闪屏页的设置
    AFNetworking content type not support
    iOS 获取本地文件的各种坑
    iOS UICollectionView 长按移动cell
  • 原文地址:https://www.cnblogs.com/qinqiu/p/8574478.html
Copyright © 2011-2022 走看看