📜 ⬆️ ⬇️

Check for non-emptyness

I looked at someone else’s code and saw several types of checking whether the variable value is empty
1) empty ($ a)
2) $ a == ''
3)! $ A
I wonder which of these methods is faster.

update: The variable $ a exists, below is just a test piece.
function gettime ()
{
$ arr = explode ('', microtime ());
return $ arr [1] + $ arr [0];
}

define ('Count', 10,000,000);
$ v = gettime ();
for ($ i = 0; $ i [= Count; ++ $ i)
if (empty ($ a))
{
}
echo ((gettime () - $ v)). "\ n";

$ v = gettime ();
for ($ i = 0; $ i [= Count; ++ $ i)
if ($ a == ''))
{
}
echo ((gettime () - $ v)). "\ n";
')
$ v = gettime ();
for ($ i = 0; $ i [= Count; ++ $ i)
if (! $ a)
{
}
echo ((gettime () - $ v)). "\ n";


The result is quite expected.
1) 4.39
2) 4.87
3) 3.79
The logical negation operation is faster than calling the built-in function for checking for emptiness and comparison operations.

However, one should not forget that this is a discrepancy of 10 million iterations .

Source: https://habr.com/ru/post/23574/


All Articles