Skip to content

Instantly share code, notes, and snippets.

@michaelbartnett
Created July 5, 2016 02:44
Show Gist options
  • Select an option

  • Save michaelbartnett/4d6fe5f48129959284473a9113098b5d to your computer and use it in GitHub Desktop.

Select an option

Save michaelbartnett/4d6fe5f48129959284473a9113098b5d to your computer and use it in GitHub Desktop.
Boxing List<T> vs Arrays. Thankfully Arrays do the right thing
using System;
using System.Collections.Generic;
using UnityEditor;
public static class TestBoxing
{
//
// Constructors
//
static TestBoxing()
{
TestBoxing.intArray = new int[42];
TestBoxing.intList = new List<int>();
for (int i = 0; i < 42; i++) {
TestBoxing.intArray[i] = i;
TestBoxing.intList.Add(i);
}
}
//
// Static Methods
//
[MenuItem("Tools/Test Boxing Arrays")]
public static void TestArrayIteration()
{
int num = 0;
int[] array = TestBoxing.intArray;
for (int i = 0; i < array.Length; i++) {
int num2 = array[i];
num += num2;
}
}
[MenuItem("Tools/Test Boxing Lists")]
public static void TestListIteration()
{
int num = 0;
foreach (int current in TestBoxing.intList) {
num += current;
}
}
}
.class public auto ansi abstract sealed TestBoxing
extends [mscorlib]System.Object
{
// Fields
.field private static int32[] intArray
.field private static class [mscorlib]System.Collections.Generic.List`1<int32> intList
// Methods
.method private specialname rtspecialname static
void .cctor () cil managed
{
// Method begins at RVA 0xe4278
// Code size 61 (0x3d)
.maxstack 5
.locals init (
[0] int32
)
IL_0000: ldc.i4.s 42
IL_0002: newarr [mscorlib]System.Int32
IL_0007: stsfld int32[] TestBoxing::intArray
IL_000c: newobj instance void class [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
IL_0011: stsfld class [mscorlib]System.Collections.Generic.List`1<int32> TestBoxing::intList
IL_0016: ldc.i4.0
IL_0017: stloc.0
IL_0018: br IL_0034
// loop start (head: IL_0034)
IL_001d: ldsfld int32[] TestBoxing::intArray
IL_0022: ldloc.0
IL_0023: ldloc.0
IL_0024: stelem.i4
IL_0025: ldsfld class [mscorlib]System.Collections.Generic.List`1<int32> TestBoxing::intList
IL_002a: ldloc.0
IL_002b: callvirt instance void class [mscorlib]System.Collections.Generic.List`1<int32>::Add(!0)
IL_0030: ldloc.0
IL_0031: ldc.i4.1
IL_0032: add
IL_0033: stloc.0
IL_0034: ldloc.0
IL_0035: ldc.i4.s 42
IL_0037: blt IL_001d
// end loop
IL_003c: ret
} // end of method TestBoxing::.cctor
.method public hidebysig static
void TestArrayIteration () cil managed
{
.custom instance void [UnityEditor]UnityEditor.MenuItem::.ctor(string) = (
01 00 18 54 6f 6f 6c 73 2f 54 65 73 74 20 42 6f
78 69 6e 67 20 41 72 72 61 79 73 00 00
)
// Method begins at RVA 0xe42c4
// Code size 37 (0x25)
.maxstack 2
.locals init (
[0] int32,
[1] int32,
[2] int32[],
[3] int32
)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldsfld int32[] TestBoxing::intArray
IL_0007: stloc.2
IL_0008: ldc.i4.0
IL_0009: stloc.3
IL_000a: br IL_001b
// loop start (head: IL_001b)
IL_000f: ldloc.2
IL_0010: ldloc.3
IL_0011: ldelem.i4
IL_0012: stloc.1
IL_0013: ldloc.0
IL_0014: ldloc.1
IL_0015: add
IL_0016: stloc.0
IL_0017: ldloc.3
IL_0018: ldc.i4.1
IL_0019: add
IL_001a: stloc.3
IL_001b: ldloc.3
IL_001c: ldloc.2
IL_001d: ldlen
IL_001e: conv.i4
IL_001f: blt IL_000f
// end loop
IL_0024: ret
} // end of method TestBoxing::TestArrayIteration
.method public hidebysig static
void TestListIteration () cil managed
{
.custom instance void [UnityEditor]UnityEditor.MenuItem::.ctor(string) = (
01 00 17 54 6f 6f 6c 73 2f 54 65 73 74 20 42 6f
78 69 6e 67 20 4c 69 73 74 73 00 00
)
// Method begins at RVA 0xe42f8
// Code size 60 (0x3c)
.maxstack 2
.locals init (
[0] int32,
[1] int32,
[2] valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>
)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldsfld class [mscorlib]System.Collections.Generic.List`1<int32> TestBoxing::intList
IL_0007: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<int32>::GetEnumerator()
IL_000c: stloc.2
.try
{
IL_000d: br IL_001e
// loop start (head: IL_001e)
IL_0012: ldloca.s 2
IL_0014: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::get_Current()
IL_0019: stloc.1
IL_001a: ldloc.0
IL_001b: ldloc.1
IL_001c: add
IL_001d: stloc.0
IL_001e: ldloca.s 2
IL_0020: call instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>::MoveNext()
IL_0025: brtrue IL_0012
// end loop
IL_002a: leave IL_003b
} // end .try
finally
{
IL_002f: ldloc.2
IL_0030: box valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<int32>
IL_0035: callvirt instance void [mscorlib]System.IDisposable::Dispose()
IL_003a: endfinally
} // end handler
IL_003b: ret
} // end of method TestBoxing::TestListIteration
} // end of class TestBoxing
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment