开发者

Which is faster in ASP.Net?

Our project is currently usi开发者_JAVA百科ng 2 ways to find a Control inside of pages. The first is to use .FindControl recursively. The other is to use LINQ like this:

(from n in Page.Controls.Cast<Control>().Descendants(c => c.Controls.Cast<Control>())
 where (n as Label != null && n.ID == "TaskIDLabel")
 select n).First() as Label;

Which uses this Extension:

static public IEnumerable<T> Descendants<T>(this IEnumerable<T> source,
                                            Func<T, IEnumerable<T>> DescendBy)
{
    foreach (T value in source)
    {
        yield return value;

        foreach (T child in DescendBy(value).Descendants<T>(DescendBy))
        {
            yield return child;
        }
    }
}

Which of these 2 methods is better? Which is faster?


I could perform testing and find out however the stopwatch class is quick and easy to use and will tell you exactly what you need to know in about 30 seconds of effort. Let us know what you find.

http://www.dotnetperls.com/stopwatch

http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx

I suggest either adding breakpoints at the end of each stopwatch or just outputting the value using Response.Write as you're only debugging.

Edit: Have you actually noticed any performance issues or are you trying to standardise and make sure you select the correct method?

If you're using .Net 4 depending on the number of controls you could perhaps look at using the parallel extensions to iterate over the list quicker which may help. Look up "AsParallel()"


Your code and FindControl are functionally different. They do not do the same thing.

FindControl does not perform a deep search, whereas your code does. From MSDN:

This method will find a control only if the control is directly contained by the specified container; that is, the method does not search throughout a hierarchy of controls within controls.

Which is better? It depends. If you don't know where on the page a control is, then your recursive methods can find it.

However, assume that you have two controls in a Panel (ID="MyPanel"): a custom UserControl (ID="MyControl") and a Label (ID="MyName"). If you call `MyPanel.FindControl("MyName"), you will get back the expected label in the panel. If you use your function, it will first search within MyControl for a Label with ID="MyName". Because of that, if MyControl happens to also contain a label with ID="MyName", it will be returned instead. This could be unexpected if a control happens to generate a child control with the same ID as what you are looking for.

As for performance, your method performs a deeper search so it has the potential to be a much more expensive operation.


This is a gut shot answer but I would say the IL code your hand wrote code will generate is probably very very similar to the code used by FindControl. One major difference however is Microsoft code is generally extremely optimized for the CLR taking advantage of lots of compiler hints to execute faster than most hand written code.

So I would personally use FindControl in a recursive call.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜