Skip to content

Instantly share code, notes, and snippets.

@alexandrnikitin
Last active November 23, 2015 18:19
Show Gist options
  • Save alexandrnikitin/ca2067e590b16c2800de to your computer and use it in GitHub Desktop.
Save alexandrnikitin/ca2067e590b16c2800de to your computer and use it in GitHub Desktop.
Generic performance
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace PerformanceProblem
{
public class Program
{
public static void Main()
{
Console.ReadKey();
Measure(new DerivedClass());
Measure(new BaseClass<object>());
}
private static void Measure(BaseClass<object> baseClass)
{
var sw = Stopwatch.StartNew();
baseClass.Run();
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds);
}
}
public class DerivedClass : BaseClass<object>
{
}
public class BaseClass<T>
{
private List<T> _list = new List<T>();
public BaseClass()
{
Enumerable.Empty<T>();
}
public void Run()
{
for (var i = 0; i < 8000000; i++)
{
if (_list.Any())
//if (_list.Any(_ => true))
{
return;
}
}
}
}
}
0:000> !u eip
Normal JIT generated code
PerformanceProblem.BaseClass`1[[System.__Canon, mscorlib]].Run()
Begin 00007ffc8c2e0bf0, size 48
*** WARNING: Unable to verify checksum for ConsoleApplication1.exe
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
00007ffc`8c2e0bf0 53 push rbx
00007ffc`8c2e0bf1 56 push rsi
00007ffc`8c2e0bf2 57 push rdi
00007ffc`8c2e0bf3 4883ec30 sub rsp,30h
00007ffc`8c2e0bf7 488bf1 mov rsi,rcx
00007ffc`8c2e0bfa 4889742420 mov qword ptr [rsp+20h],rsi
>>> 00007ffc`8c2e0bff 33ff xor edi,edi
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 46:
00007ffc`8c2e0c01 488b5e08 mov rbx,qword ptr [rsi+8]
00007ffc`8c2e0c05 488d15d4110400 lea rdx,[00007ffc`8c321de0]
00007ffc`8c2e0c0c 488b0e mov rcx,qword ptr [rsi]
00007ffc`8c2e0c0f e81c8f685f call clr!DllCanUnloadNowInternal+0x32c90 (00007ffc`eb969b30) (JitHelp: CORINFO_HELP_RUNTIMEHANDLE_CLASS)
00007ffc`8c2e0c14 488bd3 mov rdx,rbx
00007ffc`8c2e0c17 488bc8 mov rcx,rax
*** WARNING: Unable to verify checksum for C:\WINDOWS\assembly\NativeImages_v4.0.30319_64\System.Core\205e1980e1a8e9d676d162cd47ee9e45\System.Core.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\WINDOWS\assembly\NativeImages_v4.0.30319_64\System.Core\205e1980e1a8e9d676d162cd47ee9e45\System.Core.ni.dll
00007ffc`8c2e0c1a e821d9cc5c call System_Core_ni+0x2be540 (00007ffc`e8fae540) (System.Linq.Enumerable.Any[[System.__Canon, mscorlib]](System.Collections.Generic.IEnumerable`1<System.__Canon>), mdToken: 0000000006000732)
00007ffc`8c2e0c1f 0fb6c8 movzx ecx,al
00007ffc`8c2e0c22 85c9 test ecx,ecx
00007ffc`8c2e0c24 750a jne 00007ffc`8c2e0c30
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
00007ffc`8c2e0c26 ffc7 inc edi
00007ffc`8c2e0c28 81ff00127a00 cmp edi,7A1200h
00007ffc`8c2e0c2e 7cd1 jl 00007ffc`8c2e0c01
00007ffc`8c2e0c30 4883c430 add rsp,30h
00007ffc`8c2e0c34 5f pop rdi
00007ffc`8c2e0c35 5e pop rsi
00007ffc`8c2e0c36 5b pop rbx
00007ffc`8c2e0c37 c3 ret
0:000> !U eip
Normal JIT generated code
PerformanceProblem.BaseClass`1[[System.__Canon, mscorlib]].Run()
Begin 00007ffc8c2f0b00, size f3
*** WARNING: Unable to verify checksum for ConsoleApplication1.exe
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
00007ffc`8c2f0b00 4157 push r15
00007ffc`8c2f0b02 4156 push r14
00007ffc`8c2f0b04 4155 push r13
00007ffc`8c2f0b06 4154 push r12
00007ffc`8c2f0b08 57 push rdi
00007ffc`8c2f0b09 56 push rsi
00007ffc`8c2f0b0a 55 push rbp
00007ffc`8c2f0b0b 53 push rbx
00007ffc`8c2f0b0c 4883ec28 sub rsp,28h
00007ffc`8c2f0b10 48894c2420 mov qword ptr [rsp+20h],rcx
00007ffc`8c2f0b15 488bf1 mov rsi,rcx
>>> 00007ffc`8c2f0b18 33ff xor edi,edi
00007ffc`8c2f0b1a 488b1e mov rbx,qword ptr [rsi]
00007ffc`8c2f0b1d 488bcb mov rcx,rbx
00007ffc`8c2f0b20 48ba181e338cfc7f0000 mov rdx,7FFC8C331E18h
00007ffc`8c2f0b2a e80190675f call clr!DllCanUnloadNowInternal+0x32c90 (00007ffc`eb969b30) (JitHelp: CORINFO_HELP_RUNTIMEHANDLE_CLASS)
00007ffc`8c2f0b2f 488be8 mov rbp,rax
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 46:
00007ffc`8c2f0b32 4c8b7608 mov r14,qword ptr [rsi+8]
00007ffc`8c2f0b36 488bcd mov rcx,rbp
00007ffc`8c2f0b39 e80e9f675f call clr!DllCanUnloadNowInternal+0x33bac (00007ffc`eb96aa4c) (JitHelp: CORINFO_HELP_GETGENERICS_GCSTATIC_BASE)
00007ffc`8c2f0b3e 4c8bf8 mov r15,rax
00007ffc`8c2f0b41 4d8b6708 mov r12,qword ptr [r15+8]
00007ffc`8c2f0b45 4d85e4 test r12,r12
00007ffc`8c2f0b48 7555 jne 00007ffc`8c2f0b9f
00007ffc`8c2f0b4a 488bcb mov rcx,rbx
00007ffc`8c2f0b4d 48ba981f338cfc7f0000 mov rdx,7FFC8C331F98h
00007ffc`8c2f0b57 e8d48f675f call clr!DllCanUnloadNowInternal+0x32c90 (00007ffc`eb969b30) (JitHelp: CORINFO_HELP_RUNTIMEHANDLE_CLASS)
00007ffc`8c2f0b5c 488bc8 mov rcx,rax
00007ffc`8c2f0b5f e8fc38645f call clr+0x4460 (00007ffc`eb934460) (JitHelp: CORINFO_HELP_NEWSFAST)
00007ffc`8c2f0b64 4c8be0 mov r12,rax
00007ffc`8c2f0b67 4d8b2f mov r13,qword ptr [r15]
00007ffc`8c2f0b6a 4d85ed test r13,r13
00007ffc`8c2f0b6d 7508 jne 00007ffc`8c2f0b77
00007ffc`8c2f0b6f 498bcc mov rcx,r12
*** WARNING: Unable to verify checksum for C:\WINDOWS\assembly\NativeImages_v4.0.30319_64\mscorlib\dfd75630ade67ae60abbed1c6429af70\mscorlib.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\WINDOWS\assembly\NativeImages_v4.0.30319_64\mscorlib\dfd75630ade67ae60abbed1c6429af70\mscorlib.ni.dll
00007ffc`8c2f0b72 e8a98dd45e call mscorlib_ni+0xcd9920 (00007ffc`eb039920) (System.MulticastDelegate.ThrowNullThisInDelegateToInstance(), mdToken: 0000000006000616)
00007ffc`8c2f0b77 498d4c2408 lea rcx,[r12+8]
00007ffc`8c2f0b7c 498bd5 mov rdx,r13
00007ffc`8c2f0b7f e88c09645f call clr+0x1510 (00007ffc`eb931510) (JitHelp: CORINFO_HELP_ASSIGN_REF)
00007ffc`8c2f0b84 48baa8022f8cfc7f0000 mov rdx,7FFC8C2F02A8h
00007ffc`8c2f0b8e 4989542418 mov qword ptr [r12+18h],rdx
00007ffc`8c2f0b93 498d4f08 lea rcx,[r15+8]
00007ffc`8c2f0b97 498bd4 mov rdx,r12
00007ffc`8c2f0b9a e87109645f call clr+0x1510 (00007ffc`eb931510) (JitHelp: CORINFO_HELP_ASSIGN_REF)
00007ffc`8c2f0b9f 488bcb mov rcx,rbx
00007ffc`8c2f0ba2 48ba581e338cfc7f0000 mov rdx,7FFC8C331E58h
00007ffc`8c2f0bac e87f8f675f call clr!DllCanUnloadNowInternal+0x32c90 (00007ffc`eb969b30) (JitHelp: CORINFO_HELP_RUNTIMEHANDLE_CLASS)
00007ffc`8c2f0bb1 488bc8 mov rcx,rax
00007ffc`8c2f0bb4 498bd6 mov rdx,r14
00007ffc`8c2f0bb7 4d8bc4 mov r8,r12
*** WARNING: Unable to verify checksum for C:\WINDOWS\assembly\NativeImages_v4.0.30319_64\System.Core\205e1980e1a8e9d676d162cd47ee9e45\System.Core.ni.dll
*** ERROR: Module load completed but symbols could not be loaded for C:\WINDOWS\assembly\NativeImages_v4.0.30319_64\System.Core\205e1980e1a8e9d676d162cd47ee9e45\System.Core.ni.dll
00007ffc`8c2f0bba e841eacb5c call System_Core_ni+0x2bf600 (00007ffc`e8faf600) (System.Linq.Enumerable.Any[[System.__Canon, mscorlib]](System.Collections.Generic.IEnumerable`1<System.__Canon>, System.Func`2<System.__Canon,Boolean>), mdToken: 0000000006000733)
00007ffc`8c2f0bbf 84c0 test al,al
00007ffc`8c2f0bc1 7411 je 00007ffc`8c2f0bd4
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 55:
00007ffc`8c2f0bc3 4883c428 add rsp,28h
00007ffc`8c2f0bc7 5b pop rbx
00007ffc`8c2f0bc8 5d pop rbp
00007ffc`8c2f0bc9 5e pop rsi
00007ffc`8c2f0bca 5f pop rdi
00007ffc`8c2f0bcb 415c pop r12
00007ffc`8c2f0bcd 415d pop r13
00007ffc`8c2f0bcf 415e pop r14
00007ffc`8c2f0bd1 415f pop r15
00007ffc`8c2f0bd3 c3 ret
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
00007ffc`8c2f0bd4 ffc7 inc edi
00007ffc`8c2f0bd6 81ff00127a00 cmp edi,7A1200h
00007ffc`8c2f0bdc 0f8c50ffffff jl 00007ffc`8c2f0b32
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 55:
00007ffc`8c2f0be2 4883c428 add rsp,28h
00007ffc`8c2f0be6 5b pop rbx
00007ffc`8c2f0be7 5d pop rbp
00007ffc`8c2f0be8 5e pop rsi
00007ffc`8c2f0be9 5f pop rdi
00007ffc`8c2f0bea 415c pop r12
00007ffc`8c2f0bec 415d pop r13
00007ffc`8c2f0bee 415e pop r14
00007ffc`8c2f0bf0 415f pop r15
00007ffc`8c2f0bf2 c3 ret
0:000> !U eip
Normal JIT generated code
PerformanceProblem.BaseClass`1[[System.__Canon, mscorlib]].Run()
Begin 00007ffc8c2e0b00, size 50
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
00007ffc`8c2e0b00 57 push rdi
00007ffc`8c2e0b01 56 push rsi
00007ffc`8c2e0b02 53 push rbx
00007ffc`8c2e0b03 4883ec30 sub rsp,30h
00007ffc`8c2e0b07 48894c2428 mov qword ptr [rsp+28h],rcx
00007ffc`8c2e0b0c 488bf1 mov rsi,rcx
>>> 00007ffc`8c2e0b0f 33ff xor edi,edi
00007ffc`8c2e0b11 488b0e mov rcx,qword ptr [rsi]
00007ffc`8c2e0b14 48ba281c328cfc7f0000 mov rdx,7FFC8C321C28h
00007ffc`8c2e0b1e e80d90685f call clr!DllCanUnloadNowInternal+0x32c90 (00007ffc`eb969b30) (JitHelp: CORINFO_HELP_RUNTIMEHANDLE_CLASS)
00007ffc`8c2e0b23 488bd8 mov rbx,rax
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 46:
00007ffc`8c2e0b26 488bcb mov rcx,rbx
00007ffc`8c2e0b29 488b5608 mov rdx,qword ptr [rsi+8]
00007ffc`8c2e0b2d e80edacc5c call System_Core_ni+0x2be540 (00007ffc`e8fae540) (System.Linq.Enumerable.Any[[System.__Canon, mscorlib]](System.Collections.Generic.IEnumerable`1<System.__Canon>), mdToken: 0000000006000732)
00007ffc`8c2e0b32 84c0 test al,al
00007ffc`8c2e0b34 7408 je 00007ffc`8c2e0b3e
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 55:
00007ffc`8c2e0b36 4883c430 add rsp,30h
00007ffc`8c2e0b3a 5b pop rbx
00007ffc`8c2e0b3b 5e pop rsi
00007ffc`8c2e0b3c 5f pop rdi
00007ffc`8c2e0b3d c3 ret
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
00007ffc`8c2e0b3e ffc7 inc edi
00007ffc`8c2e0b40 81ff00127a00 cmp edi,7A1200h
00007ffc`8c2e0b46 7cde jl 00007ffc`8c2e0b26
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 55:
00007ffc`8c2e0b48 4883c430 add rsp,30h
00007ffc`8c2e0b4c 5b pop rbx
00007ffc`8c2e0b4d 5e pop rsi
00007ffc`8c2e0b4e 5f pop rdi
00007ffc`8c2e0b4f c3 ret
0:000> !U eip
Normal JIT generated code
PerformanceProblem.BaseClass`1[[System.__Canon, mscorlib]].Run()
Begin 006105b0, size 3c
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
006105b0 55 push ebp
006105b1 8bec mov ebp,esp
006105b3 57 push edi
006105b4 56 push esi
006105b5 53 push ebx
006105b6 8bf9 mov edi,ecx
>>> 006105b8 33db xor ebx,ebx
006105ba 384704 cmp byte ptr [edi+4],al
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 46:
006105bd 8b7704 mov esi,dword ptr [edi+4]
006105c0 8b0f mov ecx,dword ptr [edi]
006105c2 baac1f3f00 mov edx,3F1FACh
006105c7 e88a2f8174 call clr!CopyPDBs+0x374f (74e23556) (JitHelp: CORINFO_HELP_RUNTIMEHANDLE_CLASS)
006105cc 8bd0 mov edx,eax
006105ce 8bce mov ecx,esi
006105d0 e88b22cc71 call System_Core_ni+0x1c2860 (722d2860) (System.Linq.Enumerable.Any[[System.__Canon, mscorlib]](System.Collections.Generic.IEnumerable`1<System.__Canon>), mdToken: 06000732)
006105d5 85c0 test eax,eax
006105d7 7405 je 006105de
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 55:
006105d9 5b pop ebx
006105da 5e pop esi
006105db 5f pop edi
006105dc 5d pop ebp
006105dd c3 ret
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 44:
006105de 43 inc ebx
006105df 81fb00127a00 cmp ebx,7A1200h
006105e5 7cd6 jl 006105bd
C:\temp\trace\ConsoleApplication1\ConsoleApplication1\Program.cs @ 55:
006105e7 5b pop ebx
006105e8 5e pop esi
006105e9 5f pop edi
006105ea 5d pop ebp
006105eb c3 ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment