<i id='kTNEw'><tr id='kTNEw'><dt id='kTNEw'><q id='kTNEw'><span id='kTNEw'><b id='kTNEw'><form id='kTNEw'><ins id='kTNEw'></ins><ul id='kTNEw'></ul><sub id='kTNEw'></sub></form><legend id='kTNEw'></legend><bdo id='kTNEw'><pre id='kTNEw'><center id='kTNEw'></center></pre></bdo></b><th id='kTNEw'></th></span></q></dt></tr></i><div id='kTNEw'><tfoot id='kTNEw'></tfoot><dl id='kTNEw'><fieldset id='kTNEw'></fieldset></dl></div>
    <bdo id='kTNEw'></bdo><ul id='kTNEw'></ul>

        <tfoot id='kTNEw'></tfoot><legend id='kTNEw'><style id='kTNEw'><dir id='kTNEw'><q id='kTNEw'></q></dir></style></legend>

      1. <small id='kTNEw'></small><noframes id='kTNEw'>

        为什么是 Func&lt;&gt;从 Expression&lt;Func&lt;&am

        Why is Funclt;gt; created from Expressionlt;Funclt;gt;gt; slower than Funclt;gt; declared directly?(为什么是 Funclt;gt;从 Expressionlt;Funclt;gt;gt; 创建比 Func 慢直接声明?)

      2. <small id='j0ePj'></small><noframes id='j0ePj'>

            <tbody id='j0ePj'></tbody>
        • <i id='j0ePj'><tr id='j0ePj'><dt id='j0ePj'><q id='j0ePj'><span id='j0ePj'><b id='j0ePj'><form id='j0ePj'><ins id='j0ePj'></ins><ul id='j0ePj'></ul><sub id='j0ePj'></sub></form><legend id='j0ePj'></legend><bdo id='j0ePj'><pre id='j0ePj'><center id='j0ePj'></center></pre></bdo></b><th id='j0ePj'></th></span></q></dt></tr></i><div id='j0ePj'><tfoot id='j0ePj'></tfoot><dl id='j0ePj'><fieldset id='j0ePj'></fieldset></dl></div>
            <tfoot id='j0ePj'></tfoot>

              • <bdo id='j0ePj'></bdo><ul id='j0ePj'></ul>

                  <legend id='j0ePj'><style id='j0ePj'><dir id='j0ePj'><q id='j0ePj'></q></dir></style></legend>
                  本文介绍了为什么是 Func&lt;&gt;从 Expression&lt;Func&lt;&gt;&gt; 创建比 Func 慢直接声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  为什么通过 .Compile() 从 Expression 创建的 Func<> 比仅使用 Func< 慢得多;> 直接声明?

                  Why is a Func<> created from an Expression<Func<>> via .Compile() considerably slower than just using a Func<> declared directly ?

                  我刚刚从使用直接声明的 Func 更改为在应用程序 i 中从 Expression> 创建的我正在努力,我注意到性能下降了.

                  I just changed from using a Func<IInterface, object> declared directly to one created from an Expression<Func<IInterface, object>> in an app i am working on and i noticed that the performance went down.

                  我刚刚做了一个小测试,从表达式创建的 Func<> 花费几乎"两倍于直接声明的 Func<> 时间.

                  I have just done a little test, and the Func<> created from an Expression takes "almost" double the time of an Func<> declared directly.

                  在我的机器上,直接 Func<> 大约需要 7.5 秒,Expression 大约需要 12.6 秒.

                  On my machine the Direct Func<> takes about 7.5 seconds and the Expression<Func<>> takes about 12.6 seconds.

                  这是我使用的测试代码(运行 Net 4.0)

                  Here is the test code I used (running Net 4.0)

                  // Direct
                  Func<int, Foo> test1 = x => new Foo(x * 2);
                  
                  int counter1 = 0;
                  
                  Stopwatch s1 = new Stopwatch();
                  s1.Start();
                  for (int i = 0; i < 300000000; i++)
                  {
                   counter1 += test1(i).Value;
                  }
                  s1.Stop();
                  var result1 = s1.Elapsed;
                  
                  
                  
                  // Expression . Compile()
                  Expression<Func<int, Foo>> expression = x => new Foo(x * 2);
                  Func<int, Foo> test2 = expression.Compile();
                  
                  int counter2 = 0;
                  
                  Stopwatch s2 = new Stopwatch();
                  s2.Start();
                  for (int i = 0; i < 300000000; i++)
                  {
                   counter2 += test2(i).Value;
                  }
                  s2.Stop();
                  var result2 = s2.Elapsed;
                  
                  
                  
                  public class Foo
                  {
                   public Foo(int i)
                   {
                    Value = i;
                   }
                   public int Value { get; set; }
                  }
                  

                  我怎样才能恢复性能?

                  我可以做些什么来让 Expression 创建的 Func<> 像直接声明的那样执行?p>

                  How can i get the performance back ?

                  Is there anything i can do to get the Func<> created from the Expression<Func<>> to perform like one declared directly ?

                  推荐答案

                  正如其他人所提到的,调用动态委托的开销会导致您的速度变慢.在我的计算机上,我的 CPU 为 3GHz,开销约为 12ns.解决这个问题的方法是从已编译的程序集中加载方法,如下所示:

                  As others have mentioned, the overhead of calling a dynamic delegate is causing your slowdown. On my computer that overhead is about 12ns with my CPU at 3GHz. The way to get around that is to load the method from a compiled assembly, like this:

                  var ab = AppDomain.CurrentDomain.DefineDynamicAssembly(
                               new AssemblyName("assembly"), AssemblyBuilderAccess.Run);
                  var mod = ab.DefineDynamicModule("module");
                  var tb = mod.DefineType("type", TypeAttributes.Public);
                  var mb = tb.DefineMethod(
                               "test3", MethodAttributes.Public | MethodAttributes.Static);
                  expression.CompileToMethod(mb);
                  var t = tb.CreateType();
                  var test3 = (Func<int, Foo>)Delegate.CreateDelegate(
                                  typeof(Func<int, Foo>), t.GetMethod("test3"));
                  
                  int counter3 = 0;
                  Stopwatch s3 = new Stopwatch();
                  s3.Start();
                  for (int i = 0; i < 300000000; i++)
                  {
                      counter3 += test3(i).Value;
                  }
                  s3.Stop();
                  var result3 = s3.Elapsed;
                  

                  当我添加上面的代码时,result3 总是比 result1 高几分之一秒,开销大约为 1ns.

                  When I add the above code, result3 is always just a fraction of a second higher than result1, for about a 1ns overhead.

                  既然可以拥有更快的委托 (test3),为什么还要费心使用已编译的 lambda (test2)?因为通常创建动态程序集的开销要大得多,并且每次调用只为您节省 10-20ns.

                  So why even bother with a compiled lambda (test2) when you can have a faster delegate (test3)? Because creating the dynamic assembly is much more overhead in general, and only saves you 10-20ns on each invocation.

                  这篇关于为什么是 Func&lt;&gt;从 Expression&lt;Func&lt;&gt;&gt; 创建比 Func 慢直接声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)

                    <tbody id='omK0V'></tbody>

                  <small id='omK0V'></small><noframes id='omK0V'>

                    <tfoot id='omK0V'></tfoot>

                    1. <legend id='omK0V'><style id='omK0V'><dir id='omK0V'><q id='omK0V'></q></dir></style></legend>
                      <i id='omK0V'><tr id='omK0V'><dt id='omK0V'><q id='omK0V'><span id='omK0V'><b id='omK0V'><form id='omK0V'><ins id='omK0V'></ins><ul id='omK0V'></ul><sub id='omK0V'></sub></form><legend id='omK0V'></legend><bdo id='omK0V'><pre id='omK0V'><center id='omK0V'></center></pre></bdo></b><th id='omK0V'></th></span></q></dt></tr></i><div id='omK0V'><tfoot id='omK0V'></tfoot><dl id='omK0V'><fieldset id='omK0V'></fieldset></dl></div>
                        • <bdo id='omK0V'></bdo><ul id='omK0V'></ul>