Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active May 10, 2021 16:56
Show Gist options
  • Save guitarrapc/118ace926274c4eff50d0644d23c9350 to your computer and use it in GitHub Desktop.
Save guitarrapc/118ace926274c4eff50d0644d23c9350 to your computer and use it in GitHub Desktop.
// https://sharplab.io/#v2:CYLg1APgAgTAjAWAFBQMwAJboMLoN7LpGYZQAs6AsgBQCU+hxAvsi0smpjOgEICmAOwDGACwC2AQwBOAa2QEkxdAAcpASwBuEgC590UvhOAB7AQBsAnujUDtAbQC66APoSA3MkZFO/YeOkydF4MikrErugAvOgCfADu1raO+OhwADToMBmoGWQZAKwZAGwZAOzoTB6hRGzBnDba6AAyggDm2iJB1SFhxFpS6BJRLu7BSmoAZujUEgB0LQLtIlHRcOgAZOtjvYPzbR0rmbTbRAo7YVDlcFXnbOeX6AAMN8ye3fW2zfsiTcbGyl0lGcdv1BsNXC9ehNjANqA1rMNngiADypNzWMBgWjA85ESbTOYLJaHNabE64wnfQ4wY7dc443FKB7XclKO47dm9B7PYJsNhAA===
using System;
public class C {
public void M() {
}
}
public class Benchmark
{
private readonly int[] _a;
public Benchmark()
{
_a = new int[] { 1, 2, 3, 4, 5, 6, 7 };
}
public int Length()
{
var a = _a;
if (a.Length == 1 &&
a.Length == 2)
{
return 1;
}
return 0;
}
public int LengthLoop()
{
var a = _a;
for (int i = 0; i < 1; i++){
if (a.Length == 1 &&
a.Length == 2)
{
return 1;
}
}
return 0;
}
}
void Main()
{
var summary = BenchmarkRunner.Run<Benchmark>();
}
[MemoryDiagnoser]
public class Benchmark
{
private int[] _a;
[GlobalSetup]
public void Init()
{
_a = new int[] { 1, 2, 3, 4, 5, 6, 7 };
}
[Benchmark]
public int Length()
{
var a = _a;
if (a.Length == 1 &&
a.Length == 2)
{
return 1;
}
return 0;
}
[Benchmark]
public int LengthLoop()
{
var a = _a;
for (int i = 0; i < 1; i++){
if (a.Length == 1 &&
a.Length == 2)
{
return 1;
}
}
return 0;
}
}
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public class C
{
public void M()
{
}
}
public class Benchmark
{
private readonly int[] _a;
public Benchmark()
{
int[] array = new int[7];
RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
_a = array;
}
public int Length()
{
int[] a = _a;
if (a.Length == 1 && a.Length == 2)
{
return 1;
}
return 0;
}
public int LengthLoop()
{
int[] a = _a;
int num = 0;
while (num < 1)
{
if (a.Length == 1 && a.Length == 2)
{
return 1;
}
num++;
}
return 0;
}
}
[CompilerGenerated]
internal sealed class <PrivateImplementationDetails>
{
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 28)]
private struct __StaticArrayInitTypeSize=28
{
}
internal static readonly __StaticArrayInitTypeSize=28 4C816952BA53CC361D8E45BD833338DC6427E4A5D5F06EBAD5351FD46439A15A/* Not supported: data(01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00) */;
}
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
public class C
{
public void M()
{
}
}
public class Benchmark
{
private readonly int[] _a;
public Benchmark()
{
int[] array = new int[7];
RuntimeHelpers.InitializeArray(array, (RuntimeFieldHandle)/*OpCode not supported: LdMemberToken*/);
_a = array;
}
public int Length()
{
int[] a = _a;
if (a.Length == 1 && a.Length == 2)
{
return 1;
}
return 0;
}
public int LengthLoop()
{
int[] a = _a;
int num = 0;
while (num < 1)
{
if (a.Length == 1 && a.Length == 2)
{
return 1;
}
num++;
}
return 0;
}
}
[CompilerGenerated]
internal sealed class <PrivateImplementationDetails>
{
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 28)]
private struct __StaticArrayInitTypeSize=28
{
}
internal static readonly __StaticArrayInitTypeSize=28 4C816952BA53CC361D8E45BD833338DC6427E4A5D5F06EBAD5351FD46439A15A/* Not supported: data(01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 06 00 00 00 07 00 00 00) */;
}
; Core CLR v5.0.421.11614 on x86
C..ctor()
L0000: ret
C.M()
L0000: ret
Benchmark..ctor()
L0000: push esi
L0001: vzeroupper
L0004: mov esi, ecx
L0006: mov ecx, 0x63eb9d0
L000b: mov edx, 7
L0010: call 0x061f3224
L0015: mov edx, 0x189808e8
L001a: vmovdqu xmm0, [edx]
L001e: vmovdqu [eax+8], xmm0
L0023: mov ecx, [edx+0x10]
L0026: mov [eax+0x18], ecx
L0029: mov ecx, [edx+0x14]
L002c: mov [eax+0x1c], ecx
L002f: mov ecx, [edx+0x18]
L0032: mov [eax+0x20], ecx
L0035: lea edx, [esi+4]
L0038: call 0x6fb2d3b0
L003d: pop esi
L003e: ret
Benchmark.Length()
L0000: push ebp
L0001: mov ebp, esp
L0003: mov eax, [ecx+4]
L0006: mov eax, [eax+4]
L0009: cmp eax, 1
L000c: jne short L001a
L000e: cmp eax, 2
L0011: jne short L001a
L0013: mov eax, 1
L0018: pop ebp
L0019: ret
L001a: xor eax, eax
L001c: pop ebp
L001d: ret
Benchmark.LengthLoop()
L0000: push ebp
L0001: mov ebp, esp
L0003: mov eax, [ecx+4]
L0006: xor edx, edx
L0008: mov eax, [eax+4]
L000b: inc edx
L000c: test edx, edx
L000e: jle short L000b
L0010: xor eax, eax
L0012: pop ebp
L0013: ret
@guitarrapc
Copy link
Author

image

@guitarrapc
Copy link
Author

image

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