What is the fastest is_null($y) or $y == null?

Asked

Viewed 169 times

13

What’s the fastest is_null($y) or $y == null?

  • Particularly, in some cases, worrying about microtimization is unnecessary. It’s like a phrase I’ve seen around "... some programmers discover that the for is a millionth of a second faster to iterate with array than the foreach and sets up a talk saying we shouldn’t use foreach..."

  • @Wallacemaxters there are cases that are the opposite, at least in the last tests I did of micro-optimization foreach was faster.

  • Paulocosta it is likely that the difference is imperceptible, however I prefer to use $y === null (with three signs of =), because the comparison is strict.

  • In general, @Guilhermenascimento, the rule I use is the following: "started with ==, so now you have to go in the whole system. Ugly is having a part one way and another, another".

  • 1

    @Exact wallacemaxters, if the comparison can be strict, why not do it in the whole system? With a sublimetext of life and a query by regex you find all sites you need to edit.

  • Did any of the answers help you? Or are there problems with them? Comment informing the author who doubts to try to use the proposed solution. If any of the answers solved your problem, mark it as correct by clicking on

Show 1 more comment

5 answers

10


Phrase Security

The question that really must be asked is this:, "Which is safer to use?". For example, the following expressions:

"oi" == null    => false
""   == null    => true
0    == null    => true
null == null    => true

is_null("oi")   => false
is_null("")     => false
is_null(0)      => false
is_null(null)   => true

"oi" === null   => false
""   === null   => false
0    === null   => false
null === null   => true

Optimizing

The difference between === and is_null is practically irrelevant, unless you have about two million such expressions in a script to require significant optimization.

One of the comments in the PHP documentation says the following (in free translation):

Micro optimization is not worth it.

You had to make it ten million times to notice a difference, a little more than 2 seconds

$a===NULL; Lasted: 1,2424390316s

is_null($a); Lasted: 3,70693397522s

Difference = 2,46449494362s

Differs/10,000,000 = 0,0000002464494362ns

The running time between === NULL and is_null is less than 250 nanoseconds. Go optimize for something worthwhile.

Note: I did some tests and the various results did not exceed 100 nanoseconds, and the average difference is between 20 ~ 50 nanoseconds.

Completion

If you really want to optimize and have security in your code, give preference to the comparison with the triple operator:

$y === null

But there’s no point going through the code looking to optimize these expressions (unless you’re looking for the simple operator ==). Because the difference is so insignificant, that time would be better spent if you were cleaning/improving the code or studying.

More details about logical expressions: /a/69576/8493

More details about logical operators: http://php.net/manual/en/language.operators.comparison.php

6

It all depends on what you want to check, not just the performance. For example:

<?php

$str1 = "";
$str2 = null;
$str3 = 0;

var_dump(is_null($str1)); // retorna false
var_dump(is_null($str2)); // retorna true
var_dump(is_null($str3)); // retorna false

var_dump($str1 == null); // retorna true
var_dump($str2 == null); // retorna true
var_dump($str3 == null); // retorna true

?>

2

Your question is tricky because the result is never the same for:

is_null($y) ou $y == null

to compare you should do so on:

is_null($y) ou $y === null

Here yes the results will be similar. It is important to mention that there are situations where the applicability of the operator === is impossible. In cases where it is necessary to use in callbacks only the is_null can be used.

In a direct answer to the question on performance: The difference is not relevant.

But: Both are differentiated approaches to the same problem.

Because...

  1. is_null is a function that in the case of PHP runs a C. As a result some latency is obtained.
  2. In the case of PHP the difference is minimal because it is a scripting language based on C the interpretation of the code $y === null will still always originate method calls C in either case.

Opinion: I apply according to the situation, but would always choose $y === null because it is a base operator and for this nuclear reason... so the PHP tends to solve with less effort certainly.

1

There’s no difference between is_null and === null. The unifies difference and that is_null is a function and consequently,

1° The call of the function slows it down, but this speaks of a relatively insignificant time.

2° Because it is a function you can use it from callback, example:

  array_map('is_null', $array);

In the php documentation there is a benchmarking test with 10 million iterations that ends up concluding as follows, the comparison of a variable with an operator is much faster, so don’t use is_null unless you need a callback.

Php - Manual

  • +1 . The function is_null, although being a function is slower than an expression (it is not a language constructor but is a function even), it is excellent to use it as callback. It could be used in array_map, array_filter and the like :)

-1

$y == null.

In terms of performance it is not so relevant the difference, but if used frequently, really worth it just use it.

  • Why is it faster? If you can improve your response, it will certainly help more people who, like me, don’t understand anything about php.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.