【发布时间】:2018-08-24 06:19:27
【问题描述】:
我有以下程序,它从两个静态方法构造一个本地 Func。但奇怪的是,当我分析程序时,它分配了接近一百万个 Func 对象。为什么调用 Func 对象也会创建 Func 实例?
public static class Utils
{
public static bool ComparerFunc(long thisTicks, long thatTicks)
{
return thisTicks < thatTicks;
}
public static int Foo(Guid[] guids, Func<long, long, bool> comparerFunc)
{
bool a = comparerFunc(1, 2);
return 0;
}
}
class Program
{
static void Main(string[] args)
{
Func<Guid[], int> func = x => Utils.Foo(x, Utils.ComparerFunc);
var guids = new Guid[10];
for (int i = 0; i < 1000000; i++)
{
int a = func(guids);
}
}
}
【问题讨论】:
-
那是因为它处于调试模式,编译器会在构建处于发布模式时优化代码
-
@EhsanSajjad:不,我不相信是因为这个。
标签: c# performance delegates garbage-collection closures