.Net statement efficiency - XML SOAP
This is a discussion on .Net statement efficiency - XML SOAP ; How is the efficiency of this statement compared to the efficiency of
writing this out in long form?
(xml = new XmlDocument()).LoadXml("<dom />")
Is there a perfomance gain to single line statements like this?...
-
.Net statement efficiency
How is the efficiency of this statement compared to the efficiency of
writing this out in long form?
(xml = new XmlDocument()).LoadXml("<dom />")
Is there a perfomance gain to single line statements like this?
-
RE: .Net statement efficiency
Hi cd,
This is a typical C# language problem. IMO, there is not much performancce
gain to this single line statement than writing it out in seperate lines.
You can use the ILDASM to compare the two cases.
If I use
XmlDocument xml;
xml = new XmlDocument();
xml.LoadXml("<dom />");
The following IL code is generated in the release build using .NET 1.1 C#
compiler.
// Code size 18 (0x12)
.maxstack 2
.locals init (class [System.Xml]System.Xml.XmlDocument V_0)
IL_0000: newobj instance void
[System.Xml]System.Xml.XmlDocument::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ldstr "<dom />"
IL_000c: callvirt instance void
[System.Xml]System.Xml.XmlDocument::LoadXml(string)
IL_0011: ret
While if I use the single line of code.
XmlDocument xml;
(xml = new
XmlDocument()).LoadXml("<dom />")
This is the generated URL.
// Code size 16 (0x10)
.maxstack 2
IL_0000: newobj instance void
[System.Xml]System.Xml.XmlDocument::.ctor()
IL_0005: ldstr "<dom />"
IL_000a: callvirt instance void
[System.Xml]System.Xml.XmlDocument::LoadXml(string)
IL_000f: ret
These two lines are omitted.
IL_0005: stloc.0
IL_0006: ldloc.0
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
-
Re: .Net statement efficiency
So this overall decreases the size of the PE generated, and might generate
marginal efficiency gains over the entirety of a program, but nothing to
write home to grandma about?
-R
"Kevin Yu [MSFT]" <v-kevy@online.microsoft.com> wrote in message
news:Nx4Ajt2MGHA.2336@TK2MSFTNGXA01.phx.gbl...
> Hi cd,
>
> This is a typical C# language problem. IMO, there is not much performancce
> gain to this single line statement than writing it out in seperate lines.
> You can use the ILDASM to compare the two cases.
>
> If I use
>
> XmlDocument xml;
> xml = new XmlDocument();
> xml.LoadXml("<dom />");
>
> The following IL code is generated in the release build using .NET 1.1 C#
> compiler.
> // Code size 18 (0x12)
> .maxstack 2
> .locals init (class [System.Xml]System.Xml.XmlDocument V_0)
> IL_0000: newobj instance void
> [System.Xml]System.Xml.XmlDocument::.ctor()
> IL_0005: stloc.0
> IL_0006: ldloc.0
> IL_0007: ldstr "<dom />"
> IL_000c: callvirt instance void
> [System.Xml]System.Xml.XmlDocument::LoadXml(string)
> IL_0011: ret
>
> While if I use the single line of code.
>
> XmlDocument xml;
> (xml = new
> XmlDocument()).LoadXml("<dom />")
>
> This is the generated URL.
>
> // Code size 16 (0x10)
> .maxstack 2
> IL_0000: newobj instance void
> [System.Xml]System.Xml.XmlDocument::.ctor()
> IL_0005: ldstr "<dom />"
> IL_000a: callvirt instance void
> [System.Xml]System.Xml.XmlDocument::LoadXml(string)
> IL_000f: ret
>
> These two lines are omitted.
>
> IL_0005: stloc.0
> IL_0006: ldloc.0
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>
-
Re: .Net statement efficiency
You can also do this as (not using a variable at all):
new XmlDocument().LoadXml("<dom/>");
I do it this way because I'm not creating a variable to store the document,
so there's some savings there (although it's basically a malloc and an extra
32-bit pointer to store). I do it whenever possible, even if the
readability drops because it's chopping away line #s and variable creation,
but it's really a trivial matter where I use it (tends not to be an issue in
loops, as I would decl a var before I run the loop)
-
Re: .Net statement efficiency
Hi,
If this is part is called again and again, it will make better performance
in this case.
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Similar Threads
-
By Application Development in forum CSharp
Replies: 3
Last Post: 12-13-2007, 09:16 PM
-
By Application Development in forum cobol
Replies: 42
Last Post: 08-08-2007, 01:40 PM
-
By Application Development in forum Fortran
Replies: 0
Last Post: 03-21-2007, 11:35 AM
-
By Application Development in forum PROLOG
Replies: 2
Last Post: 12-08-2006, 02:05 PM
-
By Application Development in forum Inetserver
Replies: 3
Last Post: 07-23-2005, 05:05 AM