NET Just-In-Time Optimization.
نویسنده: سالار ربال
تاریخ: ۱۳۹۴/۰۱/۰۵ ۱۰:۳۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
inline type name(parameters)
{
...
} public class Vector
{
public double X { get; set; }
public double Y { get; set; }
public double Z { get; set; }
// ...
} public class MyClass
{
public int A { get; set; }
public int C;
} static void Main()
{
MyClass target = new MyClass();
int a = target.A;
Console.WriteLine("A = {0}", a);
int c = target.C;
Console.WriteLine("C = {0}", c);
} int a = target.A;
0000003e mov ecx,edi
00000040 cmp dword ptr [ecx],ecx
00000042 call dword ptr ds:[05FA29A8h]
00000048 mov esi,eax
0000004a mov dword ptr [esp+4],esi
int c = target.C;
00000098 mov edi,dword ptr [edi+4]
MyClass.get_A() looks like this:
00000000 push esi
00000001 mov esi,ecx
00000003 cmp dword ptr ds:[03B701DCh],0
0000000a je 00000011
0000000c call 76BA6BA7
00000011 mov eax,dword ptr [esi+0Ch]
00000014 pop esi
00000015 ret int a = target.A; 00000024 mov ebx,dword ptr [edi+0Ch]
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
class Program
{
const int _max = 10000000;
static void Main()
{
// ... Compile the methods.
Method1();
Method2();
int sum = 0;
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
sum += Method1();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
sum += Method2();
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.Read();
}
static int Method1()
{
// ... No inlining suggestion.
return "one".Length + "two".Length + "three".Length +
"four".Length + "five".Length + "six".Length +
"seven".Length + "eight".Length + "nine".Length +
"ten".Length;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int Method2()
{
// ... Aggressive inlining.
return "one".Length + "two".Length + "three".Length +
"four".Length + "five".Length + "six".Length +
"seven".Length + "eight".Length + "nine".Length +
"ten".Length;
}
}
Output
7.34 ns No options
0.32 ns MethodImplOptions.AggressiveInlining