CSharp/C# >> Optimal Code / Reading MSIL

by Stefan Hoffmann » Wed, 03 Sep 2008 22:23:19 GMT

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?

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 <--

CSharp/C# >> Optimal Code / Reading MSIL

by Marc Gravell » Wed, 03 Sep 2008 22:52:02 GMT


> 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

CSharp/C# >> Optimal Code / Reading MSIL

by Pavel Minaev » Thu, 04 Sep 2008 13:37:29 GMT


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.

Similar Threads

1. Optimal Buffer Size To Read A File

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 large)?  I am considering an 8K or 16K
Buffer.  The files sizes are random but range between 8K - 100K with the
occasional files being several megs.

Example:

int _READBUFFER_ = 1024 ;
fi = new FileInfo( args[ 0 ] ) ;
FileStream fs = fi.OpenRead() ;
while ( fs.Read( ByteArray, 0, _READBUFFER_ ) > 0 )
{
          myStringBuilder.Append( textConverter.GetString( ByteArray ) ) ;
}

Thanks,
Amy


2. Benchmarking STL - is my code optimal?

3. Converting MSIL to C# Source Code

I am trying to get some direction/documentation on how to convert MSIL to C# 
source code - like Reflector. Is there any documentation out there on this? 

4. MSIL code - CSharp/C#

5. Assembling and Executing MSIL from C# Code

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 that I can
successfully assemble with ilasm and execute at the DOS prompt, and
I've read up on System.Reflection.Emit and CodeDom, but I don't see how
to do this.  It looks like I would have to write a whole
System.CodeDom.Compiler.CodeCompiler; I hope I'm wrong about that,
since it seems that the whole purpose of that class is to generate the
MSIL from a source language, but I've already got the MSIL.
Thanks in advance for any pointers.

Peace,
--Carl

6. Getting MSIL code from VS.NET - CSharp/C#

7. more about MSIL code

Hi!

Will an assembly file(exe or dll) be the exact same MSIL code even after the 
JIT have compiled the MSIL code to native machine code.
I mean if a compare the whole file(exe or dll) before I run the exe and 
after I run the exe would the file be identical.

So when the CLR compile the MSIL into native machine language where is this 
native machine language written

//Tony 


8. Reading MSIL