Checking for empty arrays: count vs empty Checking for empty arrays: count vs empty arrays arrays

Checking for empty arrays: count vs empty


I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead. If you simply need to know whether or not the array is empty then use empty.


I was curious to see which one was actually faster so I made a simple script to benchmark those functions.

<?phpfunction benchmark($name, $iterations, $action){    $time=microtime(true);    for($i=0;$i<=$iterations;++$i){        $action();    }    echo $name . ' ' . round(microtime(true)-$time, 6) . "\n";}$iterations = 1000000;$x = array();$y = range(0, 10000000);$actions = array(    "Empty empty()" => function() use($x){        empty($x);    },    "Empty count()" => function() use($x){        count($x);    },    "Full empty()" => function() use($y){        empty($y);    },    "Full count()" => function() use($y){        count($y);    },    ############    "IF empty empty()" => function() use($x){        if(empty($x)){ $t=1; }    },    "IF empty count()" => function() use($x){        if(count($x)){ $t=1; }    },    "IF full empty()" => function() use($y){        if(empty($y)){ $t=1; }    },    "IF full count()" => function() use($y){        if(count($y)){ $t=1; }    },    ############    "OR empty empty()" => function() use($x){        empty($x) OR $t=1;    },    "OR empty count()" => function() use($x){        count($x) OR $t=1;    },    "OR full empty()" => function() use($y){        empty($y) OR $t=1;    },    "OR full count()" => function() use($y){        count($y) OR $t=1;    },    ############    "IF/ELSE empty empty()" => function() use($x){        if(empty($x)){ $t=1; } else { $t=2; }    },    "IF/ELSE empty count()" => function() use($x){        if(count($x)){ $t=1; } else { $t=2; }    },    "IF/ELSE full empty()" => function() use($y){        if(empty($y)){ $t=1; } else { $t=2; }    },    "IF/ELSE full count()" => function() use($y){        if(count($y)){ $t=1; } else { $t=2; }    },    ############    "( ? : ) empty empty()" => function() use($x){        $t = (empty($x) ? 1 : 2);    },    "( ? : ) empty count()" => function() use($x){        $t = (count($x) ? 1 : 2);    },    "( ? : ) full empty()" => function() use($y){        $t = (empty($y) ? 1 : 2);    },    "( ? : ) full count()" => function() use($y){        $t = (count($y) ? 1 : 2);    });foreach($actions as $name => $action){    benchmark($name, $iterations, $action);}//END

Since I was doing it I also tried to check the performance doing operations that would normally be associated with count()/empty()

Using PHP 5.4.39:

Empty empty() 0.118691Empty count() 0.218974Full empty() 0.133747Full count() 0.216424IF empty empty() 0.166474IF empty count() 0.235922IF full empty() 0.120642IF full count() 0.248273OR empty empty() 0.123875OR empty count() 0.258665OR full empty() 0.157839OR full count() 0.224869IF/ELSE empty empty() 0.167004IF/ELSE empty count() 0.263351IF/ELSE full empty() 0.145794IF/ELSE full count() 0.248425( ? : ) empty empty() 0.169487( ? : ) empty count() 0.265701( ? : ) full empty() 0.149847( ? : ) full count() 0.252891

Using HipHop VM 3.6.1 (dbg)

Empty empty() 0.210652Empty count() 0.212123Full empty() 0.206016Full count() 0.204722IF empty empty() 0.227852IF empty count() 0.219821IF full empty() 0.220823IF full count() 0.221397OR empty empty() 0.218813OR empty count() 0.220105OR full empty() 0.229118OR full count() 0.221787IF/ELSE empty empty() 0.221499IF/ELSE empty count() 0.221274IF/ELSE full empty() 0.221879IF/ELSE full count() 0.228737( ? : ) empty empty() 0.224143( ? : ) empty count() 0.222459( ? : ) full empty() 0.221606( ? : ) full count() 0.231288

Conclusions if you're using PHP:

  1. empty() is much much faster than count() in both scenarios, with an empty and populated array

  2. count() performs the same with a full or empty array.

  3. Doing a simple IF or just a Boolean operation is the same.

  4. IF/ELSE is very slightly more efficient than ( ? : ). Unless you're doing billions of iterations with expressions in the middle it is completely insignificant.

Conclusions if you're using HHVM:

  1. empty() is a teeny-weeny bit faster than count() but insignificantly so.

    [ The rest is the same as in PHP ]

In conclusion of the conclusion, if you just need to know if the array is empty always use empty();

This was just a curious test simply done without taking many things into account. It is just a proof of concept and might not reflect operations in production.


I think it's only personal preference. Some people might say empty is faster (e.g. http://jamessocol.com/projects/count_vs_empty.php) while others might say count is better since it was originally made for arrays. empty is more general and can be applied to other types.

php.net gives the following warning for count though :

count() may return 0 for a variable that isn't set, but it may also return 0 for a variable that has been initialized with an empty array. Use isset() to test if a variable is set.

In other words, if the variable is not set, you will get a notice from PHP saying it's undefined. Therefore, before using count, it would be preferable to check the variable with isset. This is not necessary with empty.