views:

765

answers:

8

Is variable assignment expensive compared to a null check? For example, is it worth checking that foo is not null before assigning it null?

if (foo != null) {
     foo = null;
}

Or is this worrying about nothing?

+23  A: 

This is a micro-micro-optimization (and possibly something handled by the compiler anyways). Don't worry about it. You'll get a far greater return by focusing on your programs actual algorithm.

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. -- Donald Knuth

Mike Douglas
Since the difference is essentially zero, which would be more clear? With the check or without?
Albert
Without the check.
Mike Douglas
@mike, you are correct. but, are we not deviating away from the actual quesiton here?the question is which would be more efficient. but we are talking about how we should not spend too much time on little things. thats good practices we are talking about and not the answer to question !
Chandan .
The problem is that the "correct" answer heavily depends on what his version of java feels like optimizing. For example, GCC >O2 realizes that there isn't a second branch there, and will remove the `cmpl`s and `je`s. Once you throw in a vm, the answer to his question is practically meaningless.
Mike Douglas
If someone asked you, whether Nike or Speedo made better swimsuits for crossing the Atlantic, wouldn't the best answer be about "good practices", and not the aerodynamic qualities of each option?
Mike Douglas
I think it is fine that everyone answered that the difference was negligible as I was not asking from an assembly code or ten-million-iterations-in-a-tight-loop angle.
Albert
Especially since I failed to realize that a compare would take longer than an assignment.
Albert
Agreed. If you do this all around your code, it will be illegible
Pablo Fernandez
+7  A: 

This is actually (very, very slightly) less efficient. Variable assignments are roughly equivalent to null checks, plus there's an extra branch possible. Not that it makes much difference.

Or is this worrying about nothing?

You got it.

Michael Myers
+3  A: 

I wouldn't worry about it - it's just extra lines of code to maintain. This is the sort of micro-optimization you should never do unless you have lots of documented proof that it's your bottleneck.

Hank Gay
+1  A: 

foo = null;

if (foo != null)
   foo = null;

If I look at the second block code I would think that you only wanted to set the foo variable to null if it was not null before, and if I look at the first code I would think that you wanted to set the variable foo to null anyway.

I know this is because of the example you wrote, but in the end this kind of micro-optimization only adds confusion (it's not worth it).

Ravi Wallau
Which version is more confusing?
Albert
+1  A: 

This will make your code so much harder to read that even if it was an optimization it wouldn't be worth the trouble.

And it's not an optimization. On most modern cpu's if statements are quite expensive.

Mendelt
Which version is more confusing?
Albert
Apparently both versions are confusing to some people.
Terry Wilcox
Ah, that's great ;)
Albert
+1  A: 

This will have little or no effect. I don't think you could even create a benchmark to demonstrate the difference.

In fact, some would argue that assigning to null at all is a code smell (see the PMD detector for NullAssignment):

Assigning a "null" to a variable (outside of its declaration) is usually bad form. Some times, the assignment is an indication that the programmer doesn't completely understand what is going on in the code. NOTE: This sort of assignment may in rare cases be useful to encourage garbage collection. If that's what you're using it for, by all means, disregard this rule :-)

In general, I'm personally leery of anything that attempts to encourage garbage collection (you almost always get effects that you didn't expect).

Bob Cross
You're right, I was unable to create a benchmark that showed any meaningful difference. I will ponder about whether or not assigning to null is poor practice at a later date. Thanks!
Albert
+2  A: 

First of all, it's micro optimization. So no need to worry much about it.

But to answer your question, you need to reduce it to just one line. (as all your code does is to set it to NULL).

foo = NULL;

Reasons being that,

Comparison is a much much costlier operation than assignment. (As comparison eats up many assembly instructions comparatively. Generally a subtraction and a comparison to zero or an XOR and comparison to zero). Assignment takes up fewer instructions.

Chandan .
+2  A: 

If you have a decent compiler they will generate identical code. If you have a crappy compiler the one with the if will be worse. On 2009 hardware assignments to variables are very cheap, and conditional branches can sometimes be expensive.

Norman Ramsey