C++ vs C# vs VB.NET - A world apart from the everday ...

A world apart from the everday ...

Assert.IsTrue(Entries.Count == 0);

C++ vs C# vs VB.NET

OK this post isn't really about this, so lemme back track a little and then you'll see where I am coming from with this.

I am the proud owner of Programming C# by Jesse Liberty (Great book on the whole). Whilst skimming through it this morning looking for something particular I noticed an interesting sidebar on page 73, Creating Objects, shown below for those who have not got the book on hand to flip to...

"VB programmers take note: while there is a performance penalty in using the VB keywords Dim and New on the same line, in C# this penalty is removed" 

Huh? OK, I don't profess to be an expert in VB.NET but I was most interested in this. Never happy to simply take things at face value I thought I'd do a little digging and see what was going on under the hood, after all if there was some kind of performance issue in VB.NET then surely it would show up in IL? So off to my trusty friend ILDASM.EXE

This VB.NET code;

    Sub Main()
        Dim doc As New System.Xml.XmlDocument
    End Sub

Produces the following IL;

 .method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
  // Code size       9 (0x9)
  .maxstack  1
  .locals init ([0] class [System.Xml]System.Xml.XmlDocument x)
  IL_0000:  nop
  IL_0001:  newobj     instance void [System.Xml]System.Xml.XmlDocument::.ctor()
  IL_0006:  stloc.0
  IL_0007:  nop
  IL_0008:  ret
} // end of method Module1::Main


And this C# code;

        static void Main(string[] args)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        }

Produces the following IL code;

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
  // Code size       7 (0x7)
  .maxstack  1
  .locals init ([0] class [System.Xml]System.Xml.XmlDocument doc)
  IL_0000:  newobj     instance void [System.Xml]System.Xml.XmlDocument::.ctor()
  IL_0005:  stloc.0
  IL_0006:  ret
} // end of method Class1::Main
 
So no rocket science here at all .... just creating a new instance of a XmlDocument to see why there would be a difference.

Any VB.NET expert wanna explain to me what Mr Liberty was referring to with his comments, I don't see it.All I see is a few "nop" statements in the VB.NET IL. Surely "nop" don't constitute a performance penalty, do they?

I went one step further and compared the IL for

Dim x as New System.Xml.XmlDocument

To

Dim x as System.Xml.XmlDocument = New System.Xml.XmlDocument

To

Dim x as System.Xml.XmlDocument
x = New System.Xml.XmlDocument

And in each of these three cases the IL produced was pretty much identical.
....

And now that we've gone through all of that i hear you screaming but what about C++. Quite observant, I have not mentioned it as yet. The only reason I included that in the title was because whilst I was digging around for the comparison between VB>NET and C# I came across an interesting post by Kate Gregory ( The American from Canada, :D private joke, guess you had to be there for that one .... ) entitled .NET Under the Hood: a Little ILDASM.

She does a similar thing but compares produced by C++, C# and VB.NET and comes to the conclusion that C# is in fact MORE like VB.NET than C++! At least in terms of what happens under the hood. So that puts to rest the argument that C# is better because it's more like C++ than VB. Stick out tongue

 

Posted: Aug 02 2007, 10:17 AM by Ryan CrawCour | with 4 comment(s)
Filed under:

Comments

Ernst Kuschke said:

Dude, C# is just better than both C++ and VB.net ;o)

# August 3, 2007 9:54 AM

Ryan CrawCour said:

Ernst; hahahaha there was never really a question of doubt! Big Smile

But does anybody know why there'd be a performance hit in VB.NET when using Dim and New on the same line? Just as a matter of interest .... Ahmed, any ideas?

# August 3, 2007 10:07 AM

ernstM said:

I think he was talking to VB6 developer not VB.NET developers. There is a difference between VB6 and VB.NET when talking about the behaviour of Dim x as New Object.

In VB6

Dim x as New Object

Is not (Hahaha IsNot, maybe I should patent that… oops it has been done) the same as

Dim x As Object

Set x = New Object

The difference comes in once you destroy the instance i.e. Set x = Nothing

In the first case if you use the instance again it will create a new instance, while in the second case it will result in a runtime error (Error 91).

It basically adds a If statement to check that the instance is not nothing before it execute the statement, some thing like (Before every call):

If x Is Nothing Then Set x = New Object

ErnstM

# August 4, 2007 10:24 AM

Ryan CrawCour said:

hmmm silly me; looking more closely at the quote from the book it does indeed say VB6. jeesh amazing how the eye sometimes sees what it wants to see!

still love the article by Kate Gregory though ...

# August 6, 2007 4:56 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: