Skip to content

Instantly share code, notes, and snippets.

@mbrenn
Created October 22, 2022 08:37
Show Gist options
  • Save mbrenn/a8d900185c4cdf7bd07c90ee0cc001ee to your computer and use it in GitHub Desktop.
Save mbrenn/a8d900185c4cdf7bd07c90ee0cc001ee to your computer and use it in GitHub Desktop.
## .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2
```assembly
; Program.StringIsLengthDynamic()
push rdi
push rsi
sub rsp,28
; var result = 0;
; ^^^^^^^^^^^^^^^
xor esi,esi
; for (var n = 0; n < 10000; n++)
; ^^^^^^^^^
xor edi,edi
mov rcx,7FFA50774F30
mov edx,4
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
mov rax,238B4367340
mov rax,[rax]
; if (empty is { Length: 0 }) { result++; }
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^
M00_L00:
mov rdx,rax
test rdx,rdx
je short M00_L01
cmp dword ptr [rdx+8],0
jne short M00_L01
inc esi
; if (nonEmpty is { Length: 0 }) { result++; }
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
M00_L01:
mov rdx,238B4367348
mov rdx,[rdx]
test rdx,rdx
je short M00_L02
cmp dword ptr [rdx+8],0
jne short M00_L02
inc esi
M00_L02:
inc edi
cmp edi,2710
jl short M00_L00
; return result;
; ^^^^^^^^^^^^^^
mov eax,esi
add rsp,28
pop rsi
pop rdi
ret
; Total bytes of code 104
```
## .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2
```assembly
; Program.StringIsNullOrEmptyDynamic()
push rdi
push rsi
sub rsp,28
; var result = 0;
; ^^^^^^^^^^^^^^^
xor esi,esi
; for (var n = 0; n < 10000; n++)
; ^^^^^^^^^
xor edi,edi
mov rcx,7FFA50784F30
mov edx,4
call CORINFO_HELP_GETSHARED_NONGCSTATIC_BASE
mov rax,2164DDA7340
mov rax,[rax]
; if (string.IsNullOrEmpty(empty)) { result++; }
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
M00_L00:
mov rdx,rax
test rdx,rdx
je short M00_L01
cmp dword ptr [rdx+8],0
jne short M00_L02
M00_L01:
inc esi
; if (string.IsNullOrEmpty(nonEmpty)) { result++; }
; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
M00_L02:
mov rdx,2164DDA7348
mov rdx,[rdx]
test rdx,rdx
je short M00_L03
cmp dword ptr [rdx+8],0
jne short M00_L04
M00_L03:
inc esi
M00_L04:
inc edi
cmp edi,2710
jl short M00_L00
; return result;
; ^^^^^^^^^^^^^^
mov eax,esi
add rsp,28
pop rsi
pop rdi
ret
; Total bytes of code 104
```
## .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2
```assembly
; Program.StringIsLength()
; var result = 0;
; ^^^^^^^^^^^^^^^
xor eax,eax
; for (var n = 0; n < 10000; n++)
; ^^^^^^^^^
xor edx,edx
mov rcx,1F698003020
mov rcx,[rcx]
mov r8,1F69800A110
mov r8,[r8]
; var x = "";
; ^^^^^^^^^^^
M00_L00:
mov r9,rcx
; if (x is { Length: 0 }) { result++; }
; ^^^^^^^^^^^^^^^^^^^^^^^
cmp dword ptr [r9+8],0
jne short M00_L01
inc eax
; x = "a";
; ^^^^^^^^
M00_L01:
mov r9,r8
; if (x is { Length: 0 }) { result++; }
; ^^^^^^^^^^^^^^^^^^^^^^^
cmp dword ptr [r9+8],0
jne short M00_L02
inc eax
M00_L02:
inc edx
cmp edx,2710
jl short M00_L00
ret
; Total bytes of code 65
```
## .NET 6.0.10 (6.0.1022.47605), X64 RyuJIT AVX2
```assembly
; Program.StringIsNullOrEmpty()
; var result = 0;
; ^^^^^^^^^^^^^^^
xor eax,eax
; for (var n = 0; n < 10000; n++)
; ^^^^^^^^^
xor edx,edx
; result++;
; ^^^^^^^^^
M00_L00:
inc eax
inc edx
cmp edx,2710
jl short M00_L00
ret
; Total bytes of code 17
```
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
[DisassemblyDiagnoser(printSource: true, exportDiff:true)]
[RyuJitX64Job]
public class Program
{
public static string empty = "";
public static string nonEmpty = "a";
[Benchmark()]
public int StringIsLengthDynamic()
{
var result = 0;
for (var n = 0; n < 10000; n++)
{
if (empty is { Length: 0 }) { result++; }
if (nonEmpty is { Length: 0 }) { result++; }
}
return result;
}
[Benchmark()]
public int StringIsNullOrEmptyDynamic()
{
var result = 0;
for (var n = 0; n < 10000; n++)
{
if (string.IsNullOrEmpty(empty)) { result++; }
if (string.IsNullOrEmpty(nonEmpty)) { result++; }
}
return result;
}
[Benchmark()]
public int StringIsLength()
{
var result = 0;
for (var n = 0; n < 10000; n++)
{
var x = "";
if (x is { Length: 0 }) { result++; }
x = "a";
if (x is { Length: 0 }) { result++; }
}
return result;
}
[Benchmark()]
public int StringIsNullOrEmpty()
{
var result = 0;
for (var n = 0; n < 10000; n++)
{
var x = "";
if (string.IsNullOrEmpty(x))
{
result++;
}
x = "a";
if (string.IsNullOrEmpty(x))
{
result++;
}
}
return result;
}
public static void Main()
{
empty = "";
nonEmpty = "a";
BenchmarkRunner.Run(typeof(Program).Assembly);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment