Skip to content

Instantly share code, notes, and snippets.

@malkia
Created November 3, 2014 16:51
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 malkia/ebf7f54063c5aec6d1b0 to your computer and use it in GitHub Desktop.
Save malkia/ebf7f54063c5aec6d1b0 to your computer and use it in GitHub Desktop.
#define FAST // Comment this out this going 7 times slower
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace netBlah1
{
class Program
{
static void Main(string[] args)
{
int x = 1024 * 1024 * 16;
Stopwatch w = new Stopwatch();
w.Start();
for (int i = 1; i < x; i++)
func1(x);
w.Stop();
Console.WriteLine(w.Elapsed.Milliseconds);
}
static int func1(int flag)
{
if (flag != 0)
return 0;
#if FAST
return 0;
#endif
//Console.WriteLine("this should've never been written, since flag != 0 is always true for our test");
if (flag == 0)
{
Dictionary<string, string> a0 = null;
Dictionary<string, string> a1 = null;
Dictionary<string, string> a2 = null;
Dictionary<string, string> a3 = null;
Dictionary<string, string> a4 = null;
Dictionary<string, string> a5 = null;
Dictionary<string, string> a6 = null;
Dictionary<string, string> a7 = null;
Dictionary<string, string> a8 = null;
Dictionary<string, string> a9 = null;
return a0.Count + a1.Count + a2.Count + a3.Count + a4.Count + a5.Count + a6.Count + a7.Count + a8.Count + a9.Count;
}
return 0;
}
}
}
@malkia
Copy link
Author

malkia commented Nov 3, 2014

Here is what the compiled code look like with FAST defined.
Time for execution: 5-6 milliseconds

--- e:\p\netBlah1\Program.cs ---------------------------------------------------
26: if (flag != 0)
00000000 55 push ebp
00000001 8B EC mov ebp,esp
00000003 50 push eax
00000004 89 4D FC mov dword ptr [ebp-4],ecx
00000007 83 3D 04 0B 80 00 00 cmp dword ptr ds:[00800B04h],0
0000000e 74 05 je 00000015
00000010 E8 AC A3 73 6B call 6B73A3C1
00000015 83 7D FC 00 cmp dword ptr [ebp-4],0
00000019 74 06 je 00000021
27: return 0;
0000001b 33 C0 xor eax,eax
0000001d 8B E5 mov esp,ebp
0000001f 5D pop ebp
00000020 C3 ret
28:
29: #if FAST
30: return 0;
00000021 33 C0 xor eax,eax
00000023 8B E5 mov esp,ebp
00000025 5D pop ebp
00000026 C3 ret

@malkia
Copy link
Author

malkia commented Nov 3, 2014

Now #undef FAST (comment it out) - 35-37 milliseconds.

--- e:\p\netBlah1\Program.cs ---------------------------------------------------
26: if (flag != 0)
00000000 55 push ebp
00000001 8B EC mov ebp,esp
00000003 57 push edi
00000004 56 push esi
00000005 83 EC 74 sub esp,74h
00000008 8B F1 mov esi,ecx
0000000a 8D 7D AC lea edi,[ebp-54h]
0000000d B9 12 00 00 00 mov ecx,12h
00000012 33 C0 xor eax,eax
00000014 F3 AB rep stos dword ptr es:[edi]
00000016 8B CE mov ecx,esi
00000018 89 4D F4 mov dword ptr [ebp-0Ch],ecx
0000001b 83 3D 04 0B BF 00 00 cmp dword ptr ds:[00BF0B04h],0
00000022 74 05 je 00000029
00000024 E8 98 A3 7C 6B call 6B7CA3C1
00000029 33 D2 xor edx,edx
0000002b 89 55 A4 mov dword ptr [ebp-5Ch],edx
0000002e 33 D2 xor edx,edx
00000030 89 55 A0 mov dword ptr [ebp-60h],edx
00000033 33 D2 xor edx,edx
00000035 89 55 9C mov dword ptr [ebp-64h],edx
00000038 33 D2 xor edx,edx
0000003a 89 55 A8 mov dword ptr [ebp-58h],edx
0000003d 33 D2 xor edx,edx
0000003f 89 55 98 mov dword ptr [ebp-68h],edx
00000042 33 D2 xor edx,edx
00000044 89 55 94 mov dword ptr [ebp-6Ch],edx
00000047 33 D2 xor edx,edx
00000049 89 55 90 mov dword ptr [ebp-70h],edx
0000004c 33 D2 xor edx,edx
0000004e 89 55 8C mov dword ptr [ebp-74h],edx
00000051 33 D2 xor edx,edx
00000053 89 55 88 mov dword ptr [ebp-78h],edx
00000056 33 D2 xor edx,edx
00000058 89 55 84 mov dword ptr [ebp-7Ch],edx
0000005b 83 7D F4 00 cmp dword ptr [ebp-0Ch],0
0000005f 74 09 je 0000006A
27: return 0;
00000061 33 C0 xor eax,eax
00000063 8D 65 F8 lea esp,[ebp-8]
00000066 5E pop esi
00000067 5F pop edi
00000068 5D pop ebp
00000069 C3 ret
28:
29: #if FAST
30: return 0;
31: #endif
32: //Console.WriteLine("this should've never been written, since flag != 0 is always true for our test");
33:
34: if (flag == 0)
0000006a 83 7D F4 00 cmp dword ptr [ebp-0Ch],0
0000006e 0F 85 09 01 00 00 jne 0000017D
35: {
36: Dictionary<string, string> a0 = null;
00000074 33 D2 xor edx,edx
00000076 89 55 A8 mov dword ptr [ebp-58h],edx
37: Dictionary<string, string> a1 = null;
00000079 33 D2 xor edx,edx
0000007b 89 55 A4 mov dword ptr [ebp-5Ch],edx
38: Dictionary<string, string> a2 = null;
0000007e 33 D2 xor edx,edx
00000080 89 55 A0 mov dword ptr [ebp-60h],edx
39: Dictionary<string, string> a3 = null;
00000083 33 D2 xor edx,edx
00000085 89 55 9C mov dword ptr [ebp-64h],edx
40: Dictionary<string, string> a4 = null;
00000088 33 D2 xor edx,edx
0000008a 89 55 98 mov dword ptr [ebp-68h],edx
41: Dictionary<string, string> a5 = null;
0000008d 33 D2 xor edx,edx
0000008f 89 55 94 mov dword ptr [ebp-6Ch],edx
42: Dictionary<string, string> a6 = null;
00000092 33 D2 xor edx,edx
00000094 89 55 90 mov dword ptr [ebp-70h],edx
43: Dictionary<string, string> a7 = null;
00000097 33 D2 xor edx,edx
00000099 89 55 8C mov dword ptr [ebp-74h],edx
44: Dictionary<string, string> a8 = null;
0000009c 33 D2 xor edx,edx
0000009e 89 55 88 mov dword ptr [ebp-78h],edx
45: Dictionary<string, string> a9 = null;
000000a1 33 D2 xor edx,edx
000000a3 89 55 84 mov dword ptr [ebp-7Ch],edx
46:
47: return a0.Count + a1.Count + a2.Count + a3.Count + a4.Count + a5.Count + a6.Count + a7.Count + a8.Count + a9.Count;
000000a6 8B 4D A8 mov ecx,dword ptr [ebp-58h]
000000a9 39 09 cmp dword ptr [ecx],ecx
000000ab E8 98 62 D6 62 call 62D66348
000000b0 89 45 F0 mov dword ptr [ebp-10h],eax
000000b3 8B 4D A4 mov ecx,dword ptr [ebp-5Ch]
000000b6 39 09 cmp dword ptr [ecx],ecx
000000b8 E8 8B 62 D6 62 call 62D66348
000000bd 89 45 EC mov dword ptr [ebp-14h],eax
000000c0 8B 45 F0 mov eax,dword ptr [ebp-10h]
000000c3 03 45 EC add eax,dword ptr [ebp-14h]
000000c6 89 45 E8 mov dword ptr [ebp-18h],eax
000000c9 8B 4D A0 mov ecx,dword ptr [ebp-60h]
000000cc 39 09 cmp dword ptr [ecx],ecx
000000ce E8 75 62 D6 62 call 62D66348
000000d3 89 45 E4 mov dword ptr [ebp-1Ch],eax
000000d6 8B 45 E8 mov eax,dword ptr [ebp-18h]
000000d9 03 45 E4 add eax,dword ptr [ebp-1Ch]
000000dc 89 45 E0 mov dword ptr [ebp-20h],eax
000000df 8B 4D 9C mov ecx,dword ptr [ebp-64h]
000000e2 39 09 cmp dword ptr [ecx],ecx
000000e4 E8 5F 62 D6 62 call 62D66348
000000e9 89 45 DC mov dword ptr [ebp-24h],eax
000000ec 8B 45 E0 mov eax,dword ptr [ebp-20h]
000000ef 03 45 DC add eax,dword ptr [ebp-24h]
000000f2 89 45 D8 mov dword ptr [ebp-28h],eax
000000f5 8B 4D 98 mov ecx,dword ptr [ebp-68h]
000000f8 39 09 cmp dword ptr [ecx],ecx
000000fa E8 49 62 D6 62 call 62D66348
000000ff 89 45 D4 mov dword ptr [ebp-2Ch],eax
00000102 8B 45 D8 mov eax,dword ptr [ebp-28h]
00000105 03 45 D4 add eax,dword ptr [ebp-2Ch]
00000108 89 45 D0 mov dword ptr [ebp-30h],eax
0000010b 8B 4D 94 mov ecx,dword ptr [ebp-6Ch]
0000010e 39 09 cmp dword ptr [ecx],ecx
00000110 E8 33 62 D6 62 call 62D66348
00000115 89 45 CC mov dword ptr [ebp-34h],eax
00000118 8B 45 D0 mov eax,dword ptr [ebp-30h]
0000011b 03 45 CC add eax,dword ptr [ebp-34h]
0000011e 89 45 C8 mov dword ptr [ebp-38h],eax
00000121 8B 4D 90 mov ecx,dword ptr [ebp-70h]
00000124 39 09 cmp dword ptr [ecx],ecx
00000126 E8 1D 62 D6 62 call 62D66348
0000012b 89 45 C4 mov dword ptr [ebp-3Ch],eax
0000012e 8B 45 C8 mov eax,dword ptr [ebp-38h]
00000131 03 45 C4 add eax,dword ptr [ebp-3Ch]
00000134 89 45 C0 mov dword ptr [ebp-40h],eax
00000137 8B 4D 8C mov ecx,dword ptr [ebp-74h]
0000013a 39 09 cmp dword ptr [ecx],ecx
0000013c E8 07 62 D6 62 call 62D66348
00000141 89 45 BC mov dword ptr [ebp-44h],eax
00000144 8B 45 C0 mov eax,dword ptr [ebp-40h]
00000147 03 45 BC add eax,dword ptr [ebp-44h]
0000014a 89 45 B8 mov dword ptr [ebp-48h],eax
0000014d 8B 4D 88 mov ecx,dword ptr [ebp-78h]
00000150 39 09 cmp dword ptr [ecx],ecx
00000152 E8 F1 61 D6 62 call 62D66348
00000157 89 45 B4 mov dword ptr [ebp-4Ch],eax
0000015a 8B 45 B8 mov eax,dword ptr [ebp-48h]
0000015d 03 45 B4 add eax,dword ptr [ebp-4Ch]
00000160 89 45 B0 mov dword ptr [ebp-50h],eax
00000163 8B 4D 84 mov ecx,dword ptr [ebp-7Ch]
00000166 39 09 cmp dword ptr [ecx],ecx
00000168 E8 DB 61 D6 62 call 62D66348
0000016d 89 45 AC mov dword ptr [ebp-54h],eax
00000170 8B 45 B0 mov eax,dword ptr [ebp-50h]
00000173 03 45 AC add eax,dword ptr [ebp-54h]
00000176 8D 65 F8 lea esp,[ebp-8]
00000179 5E pop esi
0000017a 5F pop edi
0000017b 5D pop ebp
0000017c C3 ret
48: }
49:
50: return 0;
0000017d 33 C0 xor eax,eax
0000017f 8D 65 F8 lea esp,[ebp-8]
00000182 5E pop esi
00000183 5F pop edi
00000184 5D pop ebp
00000185 C3 ret

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