• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inward vs outward negation from performance point of view  RSS feed

 
Anand Kane
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!