Is there any overhead in the use of anonymous methods?

后端 未结 8 1766
遇见更好的自我
遇见更好的自我 2021-02-15 17:27

I would like to know if there is any overhead incurred through the use of anonymous methods when creating a Background worker.

for example:

public void S         


        
相关标签:
8条回答
  • 2021-02-15 17:37

    Anonymous method can be more efficient because it is always resolved to static method.

    See delegates versus anonymous methods performance peculiarity and Performance of using static methods vs instantiating the class containing the methods.

    0 讨论(0)
  • 2021-02-15 17:39

    This is what decompiler said:

    [CompilerGenerated]
    private static DoWorkEventHandler CS$<>9__CachedAnonymousMethodDelegate1;
    
    [CompilerGenerated]
    private static void <SomeMethod1>b__0(object sender, DoWorkEventArgs e)
    {
        throw new NotImplementedException();
    }
    
    public void SomeMethod1()
    {
        BackgroundWorker worker = new BackgroundWorker();
        BackgroundWorker backgroundWorker = worker;
        backgroundWorker.DoWork += (object sender, DoWorkEventArgs e) => throw new NotImplementedException();
        worker.RunWorkerAsync();
    }
    
    public void SomeMethod2()
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += worker_DoWork;
        worker.RunWorkerAsync();
    }
    
    private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        throw new NotImplementedException();
    } 
    

    Edit:

    Looking at IL code there is only small overhead in creating/assigning method to delegate for the first time.

    0 讨论(0)
  • 2021-02-15 17:43

    Whenever an anonymous method (incl lambdas) close over variables the compiler creates a class to hold these variables for you. Whenever the delegate is created a new instance of this class is too. This obviously adds extra work for the runtime, but is generally negligible in most situations.

    0 讨论(0)
  • 2021-02-15 17:51

    I was testing this out the other day (via use of the StopWatch class). As far as I could tell there was no noticeable difference in performance between invoking a method directly...

    SomeMethod();
    

    ...or via an anonymous method...

    () => SomeMethod();
    
    0 讨论(0)
  • 2021-02-15 17:52

    First, you probably shouldn't put a lot of code into an anonymous method. It would be more readable if you create a separate method for that, or even better, several methods.

    As for the IL generated, if the lambda doesn't close over any variables, then the generated IL code is the same as if you put the code in normal named method (except that the generated method has an unspeakable name).

    On the other hand, if you do close over some variable, the compiler creates a closure class to hold that variable in a field. And field access is slightly more expensive that local variable access.

    To sum up, if you close over some variables, there is small overhead (including more objects that need to be garbage collected). In most situations, this doesn't matter and worrying about this would be premature optimization. But if you think it does matter, you should profile the code.

    0 讨论(0)
  • 2021-02-15 17:53

    It's mainly affecting readability - large amounts of code in one place are almost never good ;-)

    In terms of performance see When is optimization premature?

    0 讨论(0)
提交回复
热议问题