Is there any overhead in the use of anonymous methods?

核能气质少年 提交于 2019-12-21 04:13:22

问题


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 SomeMethod()
{
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += (sender, e) =>
    {
        //large amount of code
    }

    worker.RunWorkerAsync();
}

Would the example above be any better or worse than defining the //large amount of code in a separate method?

Is there any overhead incurred in defining the background worker method in-line, particularly if SomeMethod() is called often?


回答1:


There is a small difference in how named methods and anonumous methods are handled when you create a delegate from them.

Delegates for anonymous methods are cached, so there is a small overhead for checking if the delegate already exists in the cache. On the other hand, if you run the method more than once, it will reuse the cached delegate instead of creating a new one.

Delegates for named methods are not cached, so it will be created each time.

Other than that there is no difference. The anonumous method will be created at compile time and exists in the code just like a regular method, only with a name that only the compiler knows about.




回答2:


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.




回答3:


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.




回答4:


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();



回答5:


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?




回答6:


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.




回答7:


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.




回答8:


My concern has always been - using delegates in a loop. Turns out the compiler is smart enough to cache anonymous functions even when used in a loop.

Here's my code (10 million iterations):

var dummyVar = 0;
var sw = new Stopwatch();

//delegate inside loop
sw.Start();
for(int i=0; i<10000000; i++)
{
    Func<int, int> ax = delegate (int x) { return x++; };
    dummyVar = ax(i);
}
sw.Stop();
var ms = sw.ElapsedMilliseconds;

//delegate outside loop
Func<int, int> ax2 = delegate (int x) { return x++; };
sw.Restart();
for (int i = 0; i < 10000000; i++)
{
    dummyVar = ax2(i);
}
sw.Stop();
var ms2 = sw.ElapsedMilliseconds;

Results:

1st: 151 milliseconds

2nd: 148 milliseconds

2nd run

1st: 149 milliseconds

2nd: 175 milliseconds (SLOWER this time... who knew)



来源:https://stackoverflow.com/questions/9394950/is-there-any-overhead-in-the-use-of-anonymous-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!