Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Last active December 27, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpabich/7301628 to your computer and use it in GitHub Desktop.
Save pawelpabich/7301628 to your computer and use it in GitHub Desktop.
Long - 0.4s NullableLong - 4s Why? Have a look at Joe Albahari's presentation, around 1:02h mark. http://www.youtube.com/watch?v=aZCzG2I8Hds
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Long();
NullableLong();
Console.ReadLine();
}
private static void Long()
{
var stopwatch = Stopwatch.StartNew();
long result = 0;
for (long i = 0; i < 100000000; i++)
{
result = result + i;
}
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
}
private static void NullableLong()
{
var stopwatch = Stopwatch.StartNew();
long? result = 0;
for (long? i = 0; i < 100000000; i++)
{
result = result + i;
}
stopwatch.Stop();
Console.WriteLine(stopwatch.Elapsed);
}
}
}
@dtchepak
Copy link

dtchepak commented Nov 4, 2013

From ILSpy, Long() loop body is:

        IL_000f: nop
        IL_0010: ldloc.1
        IL_0011: ldloc.2
        IL_0012: add
        IL_0013: stloc.1
        IL_0014: nop
        IL_0015: ldloc.2
        IL_0016: ldc.i4.1
        IL_0017: conv.i8
        IL_0018: add
        IL_0019: stloc.2

        IL_001a: ldloc.2
        IL_001b: ldc.i4 100000000
        IL_0020: conv.i8
        IL_0021: clt
        IL_0023: stloc.3
        IL_0024: ldloc.3
        IL_0025: brtrue.s IL_000f

NullableLong() loop body is:

        IL_001d: nop
        IL_001e: ldloc.1
        IL_001f: stloc.3
        IL_0020: ldloc.2
        IL_0021: stloc.s CS$0$0001
        IL_0023: ldloca.s CS$0$0000
        IL_0025: call instance bool valuetype [mscorlib]System.Nullable`1<int64>::get_HasValue()
        IL_002a: ldloca.s CS$0$0001
        IL_002c: call instance bool valuetype [mscorlib]System.Nullable`1<int64>::get_HasValue()
        IL_0031: and
        IL_0032: brtrue.s IL_0040

        IL_0034: ldloca.s CS$0$0002
        IL_0036: initobj valuetype [mscorlib]System.Nullable`1<int64>
        IL_003c: ldloc.s CS$0$0002
        IL_003e: br.s IL_0054

        IL_0040: ldloca.s CS$0$0000
        IL_0042: call instance !0 valuetype [mscorlib]System.Nullable`1<int64>::GetValueOrDefault()
        IL_0047: ldloca.s CS$0$0001
        IL_0049: call instance !0 valuetype [mscorlib]System.Nullable`1<int64>::GetValueOrDefault()
        IL_004e: add
        IL_004f: newobj instance void valuetype [mscorlib]System.Nullable`1<int64>::.ctor(!0)

        IL_0054: nop
        IL_0055: stloc.1
        IL_0056: nop
        IL_0057: ldloc.2
        IL_0058: stloc.3
        IL_0059: ldloca.s CS$0$0000
        IL_005b: call instance bool valuetype [mscorlib]System.Nullable`1<int64>::get_HasValue()
        IL_0060: brtrue.s IL_006e

        IL_0062: ldloca.s CS$0$0001
        IL_0064: initobj valuetype [mscorlib]System.Nullable`1<int64>
        IL_006a: ldloc.s CS$0$0001
        IL_006c: br.s IL_007d

        IL_006e: ldloca.s CS$0$0000
        IL_0070: call instance !0 valuetype [mscorlib]System.Nullable`1<int64>::GetValueOrDefault()
        IL_0075: ldc.i4.1
        IL_0076: conv.i8
        IL_0077: add
        IL_0078: newobj instance void valuetype [mscorlib]System.Nullable`1<int64>::.ctor(!0)

        IL_007d: nop
        IL_007e: stloc.2

        IL_007f: ldloc.2
        IL_0080: stloc.3
        IL_0081: ldloca.s CS$0$0000
        IL_0083: call instance !0 valuetype [mscorlib]System.Nullable`1<int64>::GetValueOrDefault()
        IL_0088: ldc.i4 100000000
        IL_008d: conv.i8
        IL_008e: bge.s IL_0099

        IL_0090: ldloca.s CS$0$0000
        IL_0092: call instance bool valuetype [mscorlib]System.Nullable`1<int64>::get_HasValue()
        IL_0097: br.s IL_009a

        IL_0099: ldc.i4.0

        IL_009a: nop
        IL_009b: stloc.s CS$4$0003
        IL_009d: ldloc.s CS$4$0003
        IL_009f: brtrue IL_001d

@pawelpabich
Copy link
Author

Yep, Joe Albahari explains it nicely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment