性能基准测试包含,存在,并且 [英] Performance Benchmarking of Contains, Exists and Any

查看:82
本文介绍了性能基准测试包含,存在,并且的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找之间的性能基准测试包含存在任何中可用的方法列表< T> 。我想找出这只是出于好奇,因为我总是在这些困惑。在诸如这些方法如此描述定义许多问题:




  1. LINQ戒指:任何()VS包含()的巨大集合

  2. Linq。任何VS .Exists - ?请告诉我差

  3. LINQ扩展方法 - 任何()与凡()与存在()



所以我决定自己做。我将其添加为一个答案。对结果的任何更深入的了解最欢迎。我也做这个基准为数组看到的结果。


解决方案

根据文档:



List.Exists(对象的方法)




确定列表(T)是否包含元素那场比赛由指定谓词定义的
的条件。




IEnumerable.Any(扩展方法)




确定序列中的任何元素是否满足条件。




List.Contains(对象方法)




确定某元素是否在列表中。




标杆:



代码:

 静态无效的主要(字串[] args)
{
ContainsExistsAnyShort();

ContainsExistsAny();
}

私有静态无效ContainsExistsAny()
{
Console.WriteLine(***************** **********************);
Console.WriteLine(********* ContainsExistsAny ***********);
Console.WriteLine(************) ;

名单,LT; INT>名单=新名单,LT; INT>(6000000);
随机随机=新的随机();
的for(int i = 0; I< 6000000;我++)
{
list.Add(random.Next(6000000));
}
INT [] = ARR list.ToArray();

查找(列表,ARR);
}

私有静态无效ContainsExistsAnyShort()
{
Console.WriteLine(***************** **********************);
Console.WriteLine(***** ContainsExistsAnyShortRange *****);
Console.WriteLine(************) ;

名单,LT; INT>名单=新名单,LT; INT>(2000);
随机随机=新的随机();
的for(int i = 0; I< 2000;我++)
{
list.Add(random.Next(6000000));
}
INT [] = ARR list.ToArray();

查找(列表,ARR);
}

私有静态无效的发现(表< INT>列表,INT [] ARR)
{
随机随机=新的随机();
INT [] =找到新的INT [10000]
的for(int i = 0; I< 10000;我++)
{
找到[I] = random.Next(6000000);
}

秒表手表= Stopwatch.StartNew();
为(INT RPT = 0;&RPT LT; 10000; RPT ++)
{
list.Contains(找到[RPT]);
}
watch.Stop();
Console.WriteLine(列出/包含:{0:N0}毫秒,watch.ElapsedMilliseconds);

手表= Stopwatch.StartNew();
为(INT RPT = 0;&RPT LT; 10000; RPT ++)
{
list.Exists(A =>一种==找到[RPT]);
}
watch.Stop();
Console.WriteLine(列出/存在:{0:N0}毫秒,watch.ElapsedMilliseconds);

手表= Stopwatch.StartNew();
为(INT RPT = 0;&RPT LT; 10000; RPT ++)
{
list.Any(A =>一种==找到[RPT]);
}
watch.Stop();
Console.WriteLine(列出/ ANY:{0:N0}毫秒,watch.ElapsedMilliseconds);

手表= Stopwatch.StartNew();
为(INT RPT = 0;&RPT LT; 10000; RPT ++)
{
arr.Contains(找到[RPT]);
}
watch.Stop();
Console.WriteLine(数组/包含:{0:N0}毫秒,watch.ElapsedMilliseconds);

Console.WriteLine(数组没有存在);

手表= Stopwatch.StartNew();
为(INT RPT = 0;&RPT LT; 10000; RPT ++)
{
arr.Any(A =>一种==找到[RPT]);
}
watch.Stop();
Console.WriteLine(数组/ ANY:{0:N0}毫秒,watch.ElapsedMilliseconds);
}



结果



<预类=郎无prettyprint-覆盖> ************ ************
***** ContainsExistsAnyShortRange *****
******************* **********
列表/包含:96ms
列表/存在:146ms
名单/任何:381ms
阵列/包含:34ms
数组没有存在
阵列/ ANY:410ms
********************** *****************
********* ContainsExistsAny ***********
**** ***********
列表/包含:257,996ms
名单/存在:379,951ms
名单/任何:884,853ms
阵列/包含:72,486ms
数组没有存在
阵列/ ANY:1,013,303ms


I have been searching for a performance benchmarking between Contains, Exists and Any methods available in the List<T>. I wanted to find this out just out of curiosity as I was always confused among these. Many questions on SO described definitions of these methods such as:

  1. LINQ Ring: Any() vs Contains() for Huge Collections
  2. Linq .Any VS .Exists - Whats the difference?
  3. LINQ extension methods - Any() vs. Where() vs. Exists()

So I decided to do it myself. I am adding it as an answer. Any more insight on the results is most welcomed. I also did this benchmarking for arrays to see the results

解决方案

According to documentation:

List.Exists (Object method)

Determines whether the List(T) contains elements that match the conditions defined by the specified predicate.

IEnumerable.Any (Extension method)

Determines whether any element of a sequence satisfies a condition.

List.Contains (Object Method)

Determines whether an element is in the List.

Benchmarking:

CODE:

    static void Main(string[] args)
    {
        ContainsExistsAnyShort();

        ContainsExistsAny();
    }

    private static void ContainsExistsAny()
    {
        Console.WriteLine("***************************************");
        Console.WriteLine("********* ContainsExistsAny ***********");
        Console.WriteLine("***************************************");

        List<int> list = new List<int>(6000000);
        Random random = new Random();
        for (int i = 0; i < 6000000; i++)
        {
            list.Add(random.Next(6000000));
        }
        int[] arr = list.ToArray();

        find(list, arr);
    }

    private static void ContainsExistsAnyShort()
    {
        Console.WriteLine("***************************************");
        Console.WriteLine("***** ContainsExistsAnyShortRange *****");
        Console.WriteLine("***************************************");

        List<int> list = new List<int>(2000);
        Random random = new Random();
        for (int i = 0; i < 2000; i++)
        {
            list.Add(random.Next(6000000));
        }
        int[] arr = list.ToArray();

        find(list, arr);
    }

    private static void find(List<int> list, int[] arr)
    {
        Random random = new Random();
        int[] find = new int[10000];
        for (int i = 0; i < 10000; i++)
        {
            find[i] = random.Next(6000000);
        }

        Stopwatch watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 10000; rpt++)
        {
            list.Contains(find[rpt]);
        }
        watch.Stop();
        Console.WriteLine("List/Contains: {0:N0}ms", watch.ElapsedMilliseconds);

        watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 10000; rpt++)
        {
            list.Exists(a => a == find[rpt]);
        }
        watch.Stop();
        Console.WriteLine("List/Exists: {0:N0}ms", watch.ElapsedMilliseconds);

        watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 10000; rpt++)
        {
            list.Any(a => a == find[rpt]);
        }
        watch.Stop();
        Console.WriteLine("List/Any: {0:N0}ms", watch.ElapsedMilliseconds);

        watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 10000; rpt++)
        {
            arr.Contains(find[rpt]);
        }
        watch.Stop();
        Console.WriteLine("Array/Contains: {0:N0}ms", watch.ElapsedMilliseconds);

        Console.WriteLine("Arrays do not have Exists");

        watch = Stopwatch.StartNew();
        for (int rpt = 0; rpt < 10000; rpt++)
        {
            arr.Any(a => a == find[rpt]);
        }
        watch.Stop();
        Console.WriteLine("Array/Any: {0:N0}ms", watch.ElapsedMilliseconds);
    }

RESULTS

***************************************
***** ContainsExistsAnyShortRange *****
***************************************
List/Contains: 96ms
List/Exists: 146ms
List/Any: 381ms
Array/Contains: 34ms
Arrays do not have Exists
Array/Any: 410ms
***************************************
********* ContainsExistsAny ***********
***************************************
List/Contains: 257,996ms
List/Exists: 379,951ms
List/Any: 884,853ms
Array/Contains: 72,486ms
Arrays do not have Exists
Array/Any: 1,013,303ms

这篇关于性能基准测试包含,存在,并且的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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