469,613 Members | 1,646 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

IsNothing(_myVar) vs. _myVar Is Nothing

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
Nov 21 '05 #1
4 1528
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::Ge tObjectValue(object)
IL_000f: call bool
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(objec t)
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...timization.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
"Dave Taylor" <no**********@processeng.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
| 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
|
|
Nov 21 '05 #2
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/de...rykeywords.asp

I hope this explains it even more.

Cor

"Dave Taylor" <no**********@processeng.com> schreef in bericht
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #3
just test it:
this tool is great, easy to use (even I was able, hehe) and free: DevPartner
Profiler
http://www.compuware.com/products/de...01181D03071E04

"Dave Taylor" <no**********@processeng.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 21 '05 #4
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/t...us,vs.80).aspx
Of course with (1) you can simply use "Not IsNothing(_myVar)" instead...
Hope this helps
Jay

"Dave Taylor" <no**********@processeng.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
| 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
|
|
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jim Heavey | last post: by
12 posts views Thread by Steve Peterson | last post: by
5 posts views Thread by John A Grandy | last post: by
4 posts views Thread by tinman | last post: by
8 posts views Thread by MattB | last post: by
7 posts views Thread by andreas | last post: by
11 posts views Thread by Jethro | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.