的FindAll VS哪里扩展法 [英] FindAll vs Where extension-method

查看:128
本文介绍了的FindAll VS哪里扩展法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,如果一个的FindAll会比去哪儿extentionMethod更快,为什么?

I just want know if a "FindAll" will be faster than a "Where" extentionMethod and why?

例如:

myList.FindAll(item=> item.category == 5);

myList.Where(item=> item.category == 5);

哪家好?

推荐答案

好了,的FindAll 将匹配的元素,新的列表,而在哪里只返回一个懒洋洋地评估序列 - 无需复制

Well, FindAll copies the matching elements to a new list, whereas Where just returns a lazily evaluated sequence - no copying is required.

我期待,因此其中,要略高于的FindAll 即使在所得序列充分评估 - 当然还有的惰性计算战略在哪里也就是说,如果你仅看(说)的第一场比赛,也不会需要检查列表的其余部分。 (马修指出,有工作在保持状态机其中,然而,这只会有一个固定的内存成本 - 而构建一个新的列表可能需要多个阵列分配等等。)

I'd therefore expect Where to be slightly faster than FindAll even when the resulting sequence is fully evaluated - and of course the lazy evaluation strategy of Where means that if you only look at (say) the first match, it won't need to check the remainder of the list. (As Matthew points out, there's work in maintaining the state machine for Where. However, this will only have a fixed memory cost - whereas constructing a new list may require multiple array allocations etc.)

基本上,的FindAll(predicate)接近凡(predicate).ToList()不是仅仅凡(predicate)

Basically, FindAll(predicate) is closer to Where(predicate).ToList() than to just Where(predicate).

只是为了反应更马修的回答了一下,我不认为他测试了它相当不够彻底。他的predicate出现回暖的一半的项目。下面是它测试相同的名单,但有三个不同的predicates简短而完整的程序 - 一个挑选任何项目,一是选取所有项目,以及一个挑选其中的一半。在每种情况下运行测试五十次获得更长的时间。

Just to react a bit more to Matthew's answer, I don't think he's tested it quite thoroughly enough. His predicate happens to pick half the items. Here's a short but complete program which tests the same list but with three different predicates - one picks no items, one picks all the items, and one picks half of them. In each case I run the test fifty times to get longer timing.

我用 COUNT(),以确保在其中,结果充分评估。结果表明,收集大约一半的结果,两者并驾齐驱。收集没有结果,的FindAll 获胜。收集的所有的结果,其中,获胜。我发现这样一个有趣的:所有的解决方案变得更慢,因为越​​来越多的比赛中找到:的FindAll 有更多的拷贝做的,而其中,已不是返回的的MoveNext()实施在短短循环匹配值。然而,的FindAll 变慢比更快其中,呢,所以就失去了早期的领先优势。非常有趣的。

I'm using Count() to make sure that the Where result is fully evaluated. The results show that collecting around half the results, the two are neck and neck. Collecting no results, FindAll wins. Collecting all the results, Where wins. I find this intriguing: all of the solutions become slower as more and more matches are found: FindAll has more copying to do, and Where has to return the matched values instead of just looping within the MoveNext() implementation. However, FindAll gets slower faster than Where does, so loses its early lead. Very interesting.

结果:

FindAll: All: 11994
Where: All: 8176
FindAll: Half: 6887
Where: Half: 6844
FindAll: None: 3253
Where: None: 4891

(编译与/ O + /调试 - 从命令行中运行,.NET 3.5)。

(Compiled with /o+ /debug- and run from the command line, .NET 3.5.)

code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

class Test
{
    static List<int> ints = Enumerable.Range(0, 10000000).ToList();

    static void Main(string[] args)
    {
        Benchmark("All", i => i >= 0); // Match all
        Benchmark("Half", i => i % 2 == 0); // Match half
        Benchmark("None", i => i < 0); // Match none
    }

    static void Benchmark(string name, Predicate<int> predicate)
    {
        // We could just use new Func<int, bool>(predicate) but that
        // would create one delegate wrapping another.
        Func<int, bool> func = (Func<int, bool>) 
            Delegate.CreateDelegate(typeof(Func<int, bool>), predicate.Target,
                                    predicate.Method);
        Benchmark("FindAll: " + name, () => ints.FindAll(predicate));
        Benchmark("Where: " + name, () => ints.Where(func).Count());
    }

    static void Benchmark(string name, Action action)
    {
        GC.Collect();
        Stopwatch sw = Stopwatch.StartNew();
        for (int i = 0; i < 50; i++)
        {
            action();
        }
        sw.Stop();
        Console.WriteLine("{0}: {1}", name, sw.ElapsedMilliseconds);
    }
}

这篇关于的FindAll VS哪里扩展法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆