473,512 Members | 15,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

.Net statement efficiency

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


Feb 16 '06 #1
4 1300
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."

Feb 17 '06 #2
cd~
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-****@online.microsoft.com> wrote in message
news:Nx**************@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."

Feb 17 '06 #3
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)
Feb 17 '06 #4
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."

Feb 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
8292
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
5
1612
by: Fred | last post by:
Hi all, I have done a lot of experimentations using several "or" predicates within an sql select statements on mysql. My only conclusion so far is that whatever syntax or order I use it is...
6
5480
by: Hardy | last post by:
One of my customers have a sql statement totaled more than 400 lines, about 40KB. when excuted, error arrised saying "SQL0101N The statement is too long or too complex". I tried one of his...
3
4605
by: Andy_Khosravi | last post by:
I have been trying to build a user friendly search engine for a small database I have created. I'm having some particular problems with one of my date fields. Here's the setup: I'm using...
81
3147
by: candy | last post by:
hi all, Is there is any way in the C language by which I can get the address of a statement? For eg,consider the following simple program: 1. #include<stdio.h> 2. 3. int main(void){ 4. ...
335
11445
by: extrudedaluminiu | last post by:
Hi, Is there any group in the manner of the C++ Boost group that works on the evolution of the C language? Or is there any group that performs an equivalent function? Thanks, -vs
8
3487
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
8
1067
by: Ed Jay | last post by:
Is there anything incorrect or risky by writing var i = edit_array - 1; document.form1.cbt.checked=true; as document.form1.cbt-1].checked=true; --
13
11771
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5077
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.