473,320 Members | 1,920 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Optimal Code / Reading MSIL

hi @all,

I have these two getter:

public ArrayList I1
{
get
{
if (i1 == null)
{
i1 = new ArrayList();
}
return i1;
}
}

public ArrayList I2
{
get
{
i2 = i2 ?? new ArrayList();
return i2;
}
}
It seems to me, that the MSIL output for the second getter is the
shorter one. But is it also the faster one?

--
.method public hidebysig specialname instance class
[mscorlib]System.Collections.ArrayList
get_I1() cil managed
{
// Code size 41 (0x29)
.maxstack 2
.locals init ([0] class [mscorlib]System.Collections.ArrayList
CS$1$0000,
[1] bool CS$4$0001)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldfld class [mscorlib]System.Collections.ArrayList
ConsoleApplication1.Test::i1
IL_0007: ldnull
IL_0008: ceq
IL_000a: ldc.i4.0
IL_000b: ceq
IL_000d: stloc.1
IL_000e: ldloc.1
IL_000f: brtrue.s IL_001e

IL_0011: nop
IL_0012: ldarg.0
IL_0013: newobj instance void
[mscorlib]System.Collections.ArrayList::.ctor()
IL_0018: stfld class [mscorlib]System.Collections.ArrayList
ConsoleApplication1.Test::i1
IL_001d: nop
IL_001e: ldarg.0
IL_001f: ldfld class [mscorlib]System.Collections.ArrayList
ConsoleApplication1.Test::i1
IL_0024: stloc.0
IL_0025: br.s IL_0027

IL_0027: ldloc.0
IL_0028: ret
} // end of method Test::get_I1

.method public hidebysig specialname instance class
[mscorlib]System.Collections.ArrayList
get_I2() cil managed
{
// Code size 33 (0x21)
.maxstack 3
.locals init ([0] class [mscorlib]System.Collections.ArrayList
CS$1$0000)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: ldarg.0
IL_0003: ldfld class [mscorlib]System.Collections.ArrayList
ConsoleApplication1.Test::i2
IL_0008: dup
IL_0009: brtrue.s IL_0011

IL_000b: pop
IL_000c: newobj instance void
[mscorlib]System.Collections.ArrayList::.ctor()
IL_0011: stfld class [mscorlib]System.Collections.ArrayList
ConsoleApplication1.Test::i2
IL_0016: ldarg.0
IL_0017: ldfld class [mscorlib]System.Collections.ArrayList
ConsoleApplication1.Test::i2
IL_001c: stloc.0
IL_001d: br.s IL_001f

IL_001f: ldloc.0
IL_0020: ret
} // end of method Test::get_I2
--

mfG
--stefan <--
Sep 3 '08 #1
2 1620
But is it also the faster one?

Well, to answer that you need profiling, not MSIL. But in reality I
don't expect either is your bottleneck; if you are having performance
issues, this almost certainly isn't the cause - concentrate on the
code that "counts", and keep the other code as simple and maintainable
as possible. I personally find the first version more readable, as it
is clear that we only want to change (assign) the field if it is null;
this *might* also make it quicker, but that is just a guess without
profiling.

To be honest, I'd be more interested in replacing ArrayList with a
generic List<T- removing boxing / casting is likely to have more
benefit than prematurely micro-optimising this code.

Marc
Sep 3 '08 #2
"Stefan Hoffmann" <st*************@explido.dewrote in message
news:ef**************@TK2MSFTNGP05.phx.gbl...
hi @all,

I have these two getter:

public ArrayList I1
{
get
{
if (i1 == null)
{
i1 = new ArrayList();
}
return i1;
}
}

public ArrayList I2
{
get
{
i2 = i2 ?? new ArrayList();
return i2;
}
}
It seems to me, that the MSIL output for the second getter is the shorter
one. But is it also the faster one?
Since MSIL is actually JIT-compiled, and .NET JIT knows a lot of
optimizations tricks itself, it may very likely compile to the same native
code. In general, small meaningless differences in MSIL rarely have any
impact on performance.
Sep 4 '08 #3

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

Similar topics

1
by: Novice | last post by:
Hi all, I'm afraid this is the second posting of this information as I didn't get a response on the previous post. I will try to shorten my message (i.e. be more concise) in the hopes that it will...
3
by: Amy L. | last post by:
Is there a Buffer size that is optimial based on the framework or OS that is optimal when working with chunks of data? Also, is there a point where the buffer size might not be optimal (too...
7
by: carl.manaster | last post by:
Hi, I'd like to take a string containing MSIL code, assemble it, execute it, and receive the result all from my running C# application. So far I've managed to manually create some MSIL code...
15
by: Fady Anwar | last post by:
Hi while browsing the net i noticed that there is sites publishing some software that claim that it can decompile .net applications i didn't bleave it in fact but after trying it i was surprised...
9
by: Raymond Lewallen | last post by:
Is there some article out there that helps in reading MSIL? I've been looking but can't find anything, and I'm probably just looking in the wrong spots for the wrong keywords. I understand mov,...
1
by: sk | last post by:
I have the following table CREATE TABLE Readings ( ReadingTime DATETIME NOT NULL DEFAULT(GETDATE()) PRIMARY KEY, Reading int NOT NULL ) INSERT INTO Readings (ReadingTime, Reading)...
10
by: Mike | last post by:
Is it still true that the managed C++ compiler will produce much better opimizations than the C# compiler, or have some of the more global/aggressive opimizations been rolled into the 2005...
0
by: Kofi | last post by:
Hi All, I have very recently inherited a project which includes a C# and SQL Server application I have to decipher and propose enhancements and new structural changes. The original developers...
3
by: Tony Johansson | last post by:
Hello! I'm reading in a book and it says the following. "MSIL(Microsoft Intermediate Language) generated by all the .NET language compilers. This is a common standard for the binary code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.