我在这里找到了Microsoft关于此的博客(使用Expression Trees生成动态方法),并且比较了静态方法、编译后的表达式树和IL注入之间的性能。
以下是代码:
static void Main(string[] args)
{
double acc = 0;
var il = ILFact();
il.Invoke(1);
var et = ETFact();
et(1);
Stopwatch sw = new Stopwatch();
for (int k = 0; k < 10; k++)
{
long time1, time2;
sw.Restart();
for (int i = 0; i < 30000; i++)
{
var result = CSharpFact(i);
acc += result;
}
sw.Stop();
time1 = sw.ElapsedMilliseconds;
sw.Restart();
for (int i = 0; i < 30000; i++)
{
double result = il.Invoke(i);
acc += result;
}
sw.Stop();
time2 = sw.ElapsedMilliseconds;
sw.Restart();
for (int i = 0; i < 30000; i++)
{
var result = et(i);
acc += result;
}
sw.Stop();
Console.WriteLine("{0,6} {1,6} {2,6}", time1, time2, sw.ElapsedMilliseconds);
}
Console.WriteLine("\n{0}...\n", acc);
Console.ReadLine();
}
static Func<int, int> ILFact()
{
var method = new DynamicMethod(
"factorial", typeof(int),
new[] { typeof(int) }
);
var il = method.GetILGenerator();
var result = il.DeclareLocal(typeof(int));
var startWhile = il.DefineLabel();
var returnResult = il.DefineLabel();
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Stloc, result);
il.MarkLabel(startWhile);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Ble_S, returnResult);
il.Emit(OpCodes.Ldloc, result);
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Dup);
il.Emit(OpCodes.Ldc_I4_1);
il.Emit(OpCodes.Sub);
il.Emit(OpCodes.Starg_S, 0);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Stloc, result);
il.Emit(OpCodes.Br_S, startWhile);
il.MarkLabel(returnResult);
il.Emit(OpCodes.Ldloc, result);
il.Emit(OpCodes.Ret);
return (Func<int, int>)method.CreateDelegate(typeof(Func<int, int>));
}
static Func<int, int> ETFact()
{
ParameterExpression value = Expression.Parameter(typeof(int), "value");
ParameterExpression result = Expression.Parameter(typeof(int), "result");
LabelTarget label = Expression.Label(typeof(int));
BlockExpression block = Expression.Block(
new[] { result },
Expression.Assign(result, Expression.Constant(1)),
Expression.Loop(
Expression.IfThenElse(
Expression.GreaterThan(value, Expression.Constant(1)),
Expression.MultiplyAssign(result,
Expression.PostDecrementAssign(value)),
Expression.Break(label, result)
),
label
)
);
return Expression.Lambda<Func<int, int>>(block, value).Compile();
}
static int CSharpFact(int value)
{
int result = 1;
while (value > 1)
{
result *= value--;
}
return result;
}
以下是在i7-920上进行的3次运行。构建版本:Release x64
583 542 660
577 578 666
550 558 652
576 575 648
570 574 641
560 554 640
558 551 650
561 551 666
624 638 683
564 581 647
-3778851060...
482 482 557
489 490 580
514 517 606
541 537 626
551 524 641
563 555 631
552 558 644
572 541 652
591 549 652
562 552 639
-3778851060...
482 482 560
507 503 591
525 543 596
555 531 609
553 556 634
540 552 640
579 598 635
607 554 639
588 585 679
547 560 643
-3778851060...
平均值:554 549 634
静态方法与 IL(Intermediate Language)比较 - IL 比静态方法快 1% (!) 尽管原因不明
静态方法与表达式树(Expression Tree)比较 - 静态方法比表达式树快 14%
编辑(2014年2月):我刚刚在.NET 4.5和更快的CPU上运行了上述代码(稍作修改),并得到了新的结果:方法/ET - 9%,方法/IL - 4%
因此,以前的结果已经不再有效 - 静态方法调用始终更快。
*不确定是新硬件(i7-3820)还是新的.NET版本,或者我在旧测试中做错了什么*
另一个有趣的结果是,在 32位 下,完全相同的代码显示出三者之间没有任何差异。
Method IL ET
--------------------
368 382 399
367 382 399
367 382 399
367 382 400
367 383 400
367 382 399
367 383 399
367 382 399
367 382 399
367 383 400
367 382 399
367 382 399
367 382 399
367 382 399
367 383 400
367 382 400
367 383 399
367 383 400
367 382 399
367 382 400
-7557702120...
--------------------
367.05 382.30 399.35