System.Threading.Interlocked.Increment(i)
for(int i=0;i<10;i++){ Debug.WriteLine(i);}
Dim i As Integer = 0 While i < 10 System.Math.Min(System.Threading.Interlocked.Increment(i),i-1) End While
System.Math.Min(System.Threading.Interlocked.Increment(i),i-1) End While
Why not? Dim i as Integer For i = 0 to 9 Debug.WriteLine(i) Next
Or even
Dim i As Integer = 0 While i < 10 Debug.WriteLine(i) i = i + 1 End While
While i < 10
Debug.WriteLine(i)
i = i + 1
End While
Of course, this could be what is happening under the covers anyway. Why this would be so, I don't have the foggiest. Let's see if we can find out.
As you probably know, when you compile a .NET app, it gets turned into MSIL (Microsoft Intermediate Language) code, not to machine code (the JIT handles that when you run the app). There is a tool that let's you look at this language if you are so inclined, the MSIL Disassembler. Being so inclined today, let's see what our for loop looks like in MSIL (the // are comments in the MSIL that indicate the original source code)...
//000028: for(int i = 0; i<10; i++) IL_0000: ldc.i4.0 IL_0001: stloc.0 IL_0002: br.s IL_0013//000029: {//000030: Debug.WriteLine(i); IL_0004: ldloc.0 IL_0005: box [mscorlib]System.Int32 IL_000a: call void [System]System.Diagnostics.Debug::WriteLine(object)//000028: for(int i = 0; i<10; i++) IL_000f: ldloc.0 IL_0010: ldc.i4.1 IL_0011: add IL_0012: stloc.0 IL_0013: ldloc.0 IL_0014: ldc.i4.s 10 IL_0016: blt.s IL_0004//000029: {//000030: Debug.WriteLine(i);//000031: }//000032: } IL_0018: ret
You will notice that there is no reference to the Interlock class nor the Math class, which is reassuring. It's simply looping through and adding 1 to i. Time to do a little speed test in VB.NET and see what happens when we use the ugly converted code versus the vanilla implementation.
Like almost every programming sin, the results are not evident in small samples, but become glaring when you actually start doing something intensive. In fact running the 10,000 iterations on the converted code caused my little test app to crash occassionally.
The moral? If you use a code converter:a) Find a good oneb) Don't take it as the gospel truthc) Clean up after it when it makes boo-boos, especially obvious ones.
Found a couple of goodies while researching this entry. This is an article about a VS add-in to paste a chunk of C# code as VB code. Looks pretty cool.
And this is a list of research tools you can download from Microsoft. Note the reference to F#!
Remember Me
Theme design by Dean Fiala
Pick a theme: BlogXP calmBlue Candid Blue dasBlog Discreet Blog Blue Elegante essence Just Html Mono Movable Radio Blue Movable Radio Heat orangeCream Portal Project84 Slate Sound Waves Tricoleur windmill
Powered by: newtelligence dasBlog 2.1.8102.813
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Dean Fiala
E-mail