IsNothing(_myVar) vs. _myVar Is Nothing

  • Thread starter Thread starter Dave Taylor
  • Start date Start date
D

Dave Taylor

This is really not very important but something that I'm just curious
about...which is faster to execute or are they the same:

(1) If IsNothing(_myVar) Then ...

or

(2) If _myVar Is Nothing Then ...

I find myself using #1 most frequently but for no real reason other than I
like the way it looks.

-- Dave
 
Dave,
Looking at the IL (with ILDASM.EXE) I would expect #2 to be faster. As
IsNothing involves calling 2 runtime routines:

//000036: If IsNothing(_myVar) Then
IL_0009: ldloc.0
IL_000a: call object
[mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
IL_000f: call bool
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(object)
IL_0014: brfalse.s IL_0016
//000037:
//000038: End If
IL_0016: nop
//000039:
//000040: If _myVar Is Nothing Then
IL_0017: ldloc.0
IL_0018: brtrue.s IL_001a
//000041:
//000042: End If
IL_001a: nop

However! the JIT compiler may or may not inline one or both of those calls.
So the actual performance difference may not be worth worrying about.

Remember the 80/20 rule. That is 80% of the execution time of your program
is spent in 20% of your code. I will optimize (worry about performance,
memory consumption) the 20% once that 20% has been identified & proven to be
a performance problem via profiling (CLR Profiler is one profiling tool).
The use of the List(Of T) may well be outside this 20% of your code,
prematurely optimizing it is possibly a waste of time.

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware/yetOptimization.pdf

In other words if you find IsNothing(_myVar) to be more readable then _myVar
Is Nothing, then go ahead & use it. When IsNothing(_myVar) is proven to be a
performance problem in a specific routine via profiling, then I would change
that routine to use Is Nothing.

Hope this helps
Jay


| This is really not very important but something that I'm just curious
| about...which is faster to execute or are they the same:
|
| (1) If IsNothing(_myVar) Then ...
|
| or
|
| (2) If _myVar Is Nothing Then ...
|
| I find myself using #1 most frequently but for no real reason other than I
| like the way it looks.
|
| -- Dave
|
|
 
Dave,

To tell the same as Jay in other words.

The IsNothing is a function in the Microsoft.VisualBasic namespace. The
Microsoft.VisualBasic namespace is a namespace included in the framework and
although it is named Microsoft.VisualBasic namespace usable by every managed
program language (C#, J# and C++ managed).

It gives a lot of handy functions, often it are *only* wrappers around
functions in System.Net while mostly it includes as well optimizing code
(even if that optimizing is helping you to write less code).

Here is as Jay showed by the ILS probably nothing more than a wrapper.

About the 80/20 I agree completly with Jay.

http://msdn.microsoft.com/library/d.../vblr7/html/vaorivbruntimelibrarykeywords.asp

I hope this explains it even more.

Cor
 
Dave,
I should add, that I would attempt to be consistent with the project,
solution, team, department, & company. On whether I used (1) or (2).


For example: In new projects/solutions that I create I normally use (2).
However if I inherited a solution that used (1), I would probably continue
using (1) in that project...


FWIW: In VB 2005 (aka Whidbey, due out in Nov 2005) a new IsNot operator is
being introduced which lends itself well to the syntax of (2).

VS 2005 info:
http://lab.msdn.microsoft.com/vs2005/

IsNot operator:
http://msdn2.microsoft.com/library/t3bat82c(en-us,vs.80).aspx


Of course with (1) you can simply use "Not IsNothing(_myVar)" instead...


Hope this helps
Jay



| This is really not very important but something that I'm just curious
| about...which is faster to execute or are they the same:
|
| (1) If IsNothing(_myVar) Then ...
|
| or
|
| (2) If _myVar Is Nothing Then ...
|
| I find myself using #1 most frequently but for no real reason other than I
| like the way it looks.
|
| -- Dave
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top