si (valor == nulo) vs si (nulo == valor) [duplicado]

Posible duplicado:
¿Por qué a menudo se ve "nulo! = Variable" en lugar de "variable! = Nulo" en C #?

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

 if(value == null)

y

 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?

preguntado el 08 de noviembre de 11 a las 10:11

And you think performance characteristics will hold over 3 very different languages? -

i dont think there is a difference in c# but the easiest way is just to write some small code and see what happens -

if you're asking this question, your whole idea of performance optimization is wrong -

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? -

here is the answer I wrote to somewhat similar question: stackoverflow.com/questions/2437144/… -

7 Respuestas

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.

respondido 08 nov., 11:14

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.

respondido 08 nov., 11:14

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. - Tu sentido común

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

respondido 25 nov., 14:08

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.

respondido 08 nov., 11:14

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

respondido 08 nov., 11:14

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 = en lugar de ==. I personally do find it less readable though.

respondido 08 nov., 11:14

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.

respondido 08 nov., 11:14

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.