惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
博客园 - 司徒正美
The GitHub Blog
The GitHub Blog
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
Google DeepMind News
Google DeepMind News
博客园 - 【当耐特】
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog
B
Blog RSS Feed
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
有赞技术团队
有赞技术团队
Jina AI
Jina AI
GbyAI
GbyAI

博客园 - dragonpig

Html 5 Canvas绘制分形图Mandelbrot const string 和 static readonly string的区别 SqlServer: Top N per Group 微软的BinarySearch 通过CTE实现Split CSV 通过SQL CTE计算Fibonacci 当json.js遇见dynamic.net烂尾篇 .NET线程安全泛型Singleton 跨域访问Cookie WCF JSON和AspnetCompatibility的配置 Windows安装Memcached node.js初体验 教你如何制作Silverlight Visual Tree Inspector 一道非常有趣的概率题 教你30秒打造强类型ASP.NET数据绑定 当json.js遇见dynamic.net [0] 用Silverlight做雷达图 C#运算符重载不是没有用武之地 随机排列算法
.net中反射、emit、expression和dynamic的性能比较
dragonpig · 2011-05-01 · via 博客园 - dragonpig

class Student
{
public string Name { get; set; }
}
static double Test(int loop, Student stu, Func<Student, string> action)
{
var watch
= Stopwatch.StartNew();
string s = null;
for (var i = 0; i < loop; i++)
s
= action(stu);return watch.ElapsedTicks;
}
static Func<Student, string> NativeGetter()
{
return s => s.Name;
}
static Func<Student, string> ReflectedGetter()
{
var type
= typeof (Student);
var prop
= type.GetProperty("Name");return s => (string)prop.GetValue(s, null);
}
static Func<Student, string> EmittedGetter()
{
var dm
= new DynamicMethod(name: "EmittedGetter", returnType: typeof(string), parameterTypes: new[] { typeof(Student) }, owner: typeof(Student));

var type

= typeof (Student);
var prop
= type.GetMethod("get_Name");
var il
= dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Call, prop);
il.Emit(OpCodes.Ret);
return (Func<Student, string>)dm.CreateDelegate(typeof(Func<Student, string>));
}
static Func<Student, string> ExpressionGetter()
{
var type
= typeof(Student);
var prop
= type.GetMethod("get_Name");

ParameterExpression pa

= Expression.Parameter(typeof(Student));
Expression body
= Expression.Call(pa, prop);return Expression.Lambda<Func<Student, string>>(body, pa).Compile();
}
static Func<Student, string> DynamicGetter()
{
return s => { dynamic d = s; return d.Name; };
}

[MethodImpl(MethodImplOptions.NoOptimization)]

public static void Run()
{
const int loop = 5000000;

var stu

= new Student {Name = "Mike"};

var dynamic

=
Test(loop, stu, DynamicGetter());

var expression

=
Test(loop, stu, ExpressionGetter());

var native

=
Test(loop, stu, NativeGetter());

var emitted

=
Test(loop, stu, EmittedGetter());

var reflected

=
Test(loop, stu, ReflectedGetter());

Console.WriteLine(

"native:{0}\ndynamic:{1}\nemit:{2}\nexpression:{3}\nreflection:{4}", 1, dynamic / native, emitted / native, expression / native, reflected / native);

Console.ReadKey();
}

测试结果

1. 当循环次数比较小的时候, loop=1000

native:1
dynamic:540.444964871194
emit:0.704918032786885
expression:0.224824355971897
reflection:8.37002341920375

2. loop=5000000

native:1
dynamic:4.37328053807767
emit:0.96159470600998
expression:1.00412887828162
reflection:35.909097418095