Click here to Skip to main content
15,889,833 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Dear Friends,

I have worked on the delegates and know that they act as pointer to functions and all the members initialization part and function initialization part is done before actually the function is called by delegate object which actually makes it faster to execute. like binding data to grid, filling drop-down etc.

My question here is that if there is one function which is made inside another function and if i call both the function with the help of delegate then will the execution of outer function which is actually doing the action will be faster or not.

e.g.,

public void method()
{
----------------
----------------
some code
method1();
}

now if i am calling the method() function with delegate and also calling the inner function (method1()) with the same delegate then will the execution of the function method() will be more faster as comparison to if i don't call the inner function through delegate.

Thanks

Varun Sareen
Posted
Comments
Sergey Alexandrovich Kryukov 18-Nov-11 1:27am    
When you ask such questions, please create and post some minimalistic code sample, otherwise it's very hard to read and understand your post.
--SA

First of all, your understanding of delegates is at least naive. A delegate does not act as a pointer to a function. First of all, there are no pointers in .NET (unless we talk unsafe code), there are references which is not the same; and a delegate instance does hold a pointer to the method, but also a reference to the instance of the class/structure, "this". Also, a delegate instance is an instance of some class which carries a whole container of such "pointers" called invocation list.

This is explained in my article Dynamic Method Dispatcher[^].

Now, let's sort out the issues with "inner function". There are no inner methods in C#, but there is a way to mimic such thing using delegates. This is explained in my Tips & Tricks article Hide Ad-hoc Methods Inside the Calling Method’s Body[^].

[EDIT]

Using inner functions in general and their implementations in the form of anonymous delegates opens up a way to such an interesting phenomenon as closure.

Please see http://en.wikipedia.org/wiki/Closure_(computer_science)[^].

In my past solution, I provided my own code sample and some explanations: Looping for creating new thread give some problem[^].

[END EDIT]

Please look at the code samples in the article referenced above. There, AdjustLayout is you "outer method", adjustMargins is your inner method, and AdjustMargins in first sample is a "regular" (not inner) variant of the same method.

A variant with "inner method" cannot be faster then a "regular" approach, because with "inner method" approach you first create an instance of a delegate and only after that invoke (call) the delegate instance. It certainly have some overhead. I think the performance loss will be insignificant for most applications.

When you say "faster as comparison to if I don't call the inner function through delegate", you probably don't mean not calling it at all but calling a "regular" version of the same method, because of course not calling anything is always faster than calling something. :-)

You can easily test performance. Just use the class System.Diagnostics.Stopwatch, see MSDN help page and code sample: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx[^]. If you do, please share your observations.

—SA
 
Share this answer
 
v2
Hi varun,
Delegate Performance[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900