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

推荐订阅源

P
Privacy International News Feed
Martin Fowler
Martin Fowler
D
Docker
Y
Y Combinator Blog
云风的 BLOG
云风的 BLOG
U
Unit 42
T
Tailwind CSS Blog
J
Java Code Geeks
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
阮一峰的网络日志
阮一峰的网络日志
WordPress大学
WordPress大学
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
美团技术团队
F
Fortinet All Blogs
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Recorded Future
Recorded Future
N
Netflix TechBlog - Medium
Google DeepMind News
Google DeepMind News
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
Microsoft Security Blog
Microsoft Security Blog
N
News and Events Feed by Topic
I
Intezer
TaoSecurity Blog
TaoSecurity Blog
NISL@THU
NISL@THU
小众软件
小众软件
博客园 - 聂微东
博客园 - Franky
有赞技术团队
有赞技术团队
P
Palo Alto Networks Blog
爱范儿
爱范儿
H
Hacker News: Front Page
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cisco Blogs
P
Proofpoint News Feed
I
InfoQ
Google DeepMind News
Google DeepMind News
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Vercel News
Vercel News
H
Heimdal Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
量子位

博客园 - 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