This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Inward vs outward negation from performance point of view Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Inward vs outward negation from performance point of view" Watch "Inward vs outward negation from performance point of view" New topic
Author

Inward vs outward negation from performance point of view

Anand Kane
Greenhorn

Joined: Mar 15, 2005
Posts: 10
Hi All

Here is a question I was asked in one of the interviews. Which of the following two performs better,

var!=null or !(var==null)?

The interviewer told me that the latter performs better. I haven't heard or read any such thing before. So I will appreciate if someone can confirm the interviewer's opinion.

Thanks
Anand
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Hi Anand,

Welcome to the Ranch!

That is actually a very good question for interviews. Although, personally, I would never use

I would prefer,

It provides better readability. However, if anyone has some other insights regarding performance I would be very interested in understanding them.


Don't walk as if you rule the world, walk as if you don't care who rules it...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

To be honest, unless you're programming time-critical code, you won't notice any difference. Changing var != null into !(var == null) because it may be microseconds (more likely nanoseconds) faster is micro-optimization.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

OK, I wanted to know myself, so I wrote this little test program:
I then used javap -c to disassemble it:
Both steps use ifnull, getstatic, aload_1 and invokevirtual. In fact, when I use JAD to decompile it, the !(s == null) is transformed into s != null. So my conclusion is: there is no difference.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
Furthermore, on many systems there will be absolutely no difference at all. I just tested using javac 1.6.0_20 on my MacBook Pro, compiling the following program:

I then ran "javap -c Negation" and got the following output:

Evidently the methods test1() and test2() compiled to exactly the same code. There will be no performance difference between these two methods. Maybe on some other systems there will be, if the compiler is not smart enough to recognize that the code is equivalent. But I think your interviewer gave a bad answer in this case, and I am very suspicious of the reasoning they were using.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2782
    
    2
Curse you, Robtimus.

Well, this is further evidence that the code is equivalent on more than one platform. Well, unless Rob is also using a Mac with JDK 6.
Anand Kane
Greenhorn

Joined: Mar 15, 2005
Posts: 10
Thank you all for posting responses with clear evidence. I posted this particular question because I wasn't sure of it. There were few more to make me realize that there was no point arguing with that interviewer. And the company is one the biggest brandnames across the globe.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32694
    
    4
Mike Simmons wrote:Curse you, Robtimus. . . .
You only say that sort of thing when he beats you by 4 seconds, not 4 minutes. Though, come to think of it, I did manage to beat him by about two hours on Tuesday . . .
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

Mike Simmons wrote:Curse you, Robtimus.

To be honest, I prefer your example. Because you used two different methods it's clearer to see the boundaries of both pieces of code.

Well, this is further evidence that the code is equivalent on more than one platform. Well, unless Rob is also using a Mac with JDK 6.

Windows XP with JDK 6.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Inward vs outward negation from performance point of view
 
Similar Threads
Doubts in Checking For Null Variable to avoid NullPointerException
when to initialise or instantiate the Vector
File not found error when loading mod_jserv.so
java performance ( comparision operator)
Date validation in struts....