7

Possible Duplicate:
Why does one often see “null != variable” instead of “variable != null” in C#?

This is more a curiosity question, is there any performance difference between the statement.

 if(value == null)

and

 if(null == value)

is it noticeable, I use c#, PHP & javascript quite often and I remember someone saying if(null == value) was faster, but is it really?

I will soon be starting development on an application that will parse huge quantities of data in the region of Terabytes so even if the performance gain is in the millisecond range it could have an impact. Anyone have any ideas?

Community
  • 1
  • 1
kamui
  • 3,339
  • 3
  • 26
  • 44
  • 3
    And you think performance characteristics will hold over 3 very different languages? – H H Nov 08 '11 at 10:28
  • 1
    i dont think there is a difference in c# but the easiest way is just to write some small code and see what happens – Ivan Crojach Karačić Nov 08 '11 at 10:28
  • AFAIK, you can't equate `NULL` with `NULL` or other defined variables ... cause NULL is undefined. – Rahul Nov 08 '11 at 10:29
  • @Rahul: In most programming languages, null is defined as null, and in PHP and JS it's a falsy value. You're probably thinking of `NULL` in SQL... – BoltClock Nov 08 '11 at 10:30
  • While the question is interesting, I'm not sure if such premature optimization will actually benefit the development process. When writing such an application you need to think about performance, obviously, but I think this goes way too far, because if you are this concerned about performance, you really shouldn't use PHP. Also, writing your code with performance in mind and later profiling it extensively will IMHO lead to better result to writing incredibly complex, performance optimized code in the first place. – fresskoma Nov 08 '11 at 10:32
  • 2
    if you're asking this question, your whole idea of performance optimization is wrong – Your Common Sense Nov 08 '11 at 10:32
  • Henk Holterman - I only mentioned the other languages in case this was something which only applied to javaScript or PHP, and that was why it had been said, being client-side and just-in-time compiled respectively, they way the statement was interpreted could be different. As @duffymo pointed out it is probably just a legacy practice with no real performance impact. – kamui Nov 08 '11 at 10:59
  • 1
    Col. Shrapnel "if you're asking this question, your whole idea of performance optimization is wrong" - I am keen to learn, do you have any suggestions? – kamui Nov 08 '11 at 11:11
  • 1
    here is the answer I wrote to somewhat similar question: http://stackoverflow.com/questions/2437144/opening-closing-tags-performance/2437413#2437413 – Your Common Sense Nov 08 '11 at 12:36

7 Answers7

23

I doubt that there's a measurable performance advantage either way. I'll bet the person who told you this didn't have any hard data.

As far as I know, it's a historical artifact, a technique from C and C++ to guard against this mistake:

if (c = null) {
}

The compiler will catch this if you reverse the arguments, since you can't assign something to null.

duffymo
  • 305,152
  • 44
  • 369
  • 561
5

There is absolutely no performance difference to be expected.

The only reason the (ugly) form (if null == value) is used is to avoid a C/C++ specific typo:

if (value = null)  // compiles but _assigns_ null to value and always returns false
    ...  // never executed
H H
  • 263,252
  • 30
  • 330
  • 514
5

I profiled both over 100 million iterations.

if(value == null)
-- 6.85175704956 seconds (68.5 nano-seconds each)
if(null == value)
-- 6.75543808937 seconds (67.5 nano-seconds each)

So it's up to you if 1 nanosecond is enough of a gain.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • 11
    Your tests is more a disservice than a real help. You made it out of assumption that such a test can prove anything, which is obviously wrong: First, doing profiling tests without a reason makes no sense. It will create another delusion among amateur users and nothing else. Second, you didn't take measurement errors into account. In a real test, not such a artificial one. you'll be unable to see any difference, as a measurement error will exceed the result by a mile. Posting such a "profiling results" cannot do any good, for whatever reason it was made. – Your Common Sense Nov 08 '11 at 12:46
4

I think it's fair to expect there will be no difference: it's such a simple case that if there was a difference the compiler would make sure to generate the fastest version anyway.

Philippe
  • 9,582
  • 4
  • 39
  • 59
2

Yes I also think there will be no difference, but personally I prefer if(value == null)

Upul Bandara
  • 5,973
  • 4
  • 37
  • 60
1

Optimizations like this will gain you a clock cycle at best (I wouldn't know why though). But most likely if it is any better, then the compiler will optimize it for you.

The reason to put any literal (such as null) first is that you can not accidentally assign the value if you do = instead of ==. I personally do find it less readable though.

Thirler
  • 20,239
  • 14
  • 63
  • 92
0

I'm not sure if it's faster or not but some languages allow you to assign in an if statement without warning you so if you do:

if (value = null) by mistake you could wipe your variable, whereas the other way around wouldn't cause weird issues further on down the execution path.

Russell Troywest
  • 8,635
  • 3
  • 35
  • 40