简体   繁体   English

将数据添加到集合中,哪个效率更高?

[英]Adding data to collection, which is more efficient?

I am adding data to a collection, but I don't know which is more efficient. 我正在将数据添加到集合中,但我不知道哪个更有效。

Using a foreach loop, 使用foreach循环,

internal class CollectionClass
{
    public List<string> ListData { get; set; }

    public CollectionClass()
    {
        ListData = new List<string>();
        string[] names = { "Matt", "Joanne", "Robert" };
        foreach (var item in names)
        {
            ListData.Add(item);
        }
    }
}

Or is this more efficent? 还是更有效?

internal class CollectionClass
{
    public List<string> ListData { get; set; }

    public CollectionClass()
    {           
        ListData.Add("Matt");
        ListData.Add("Joanne");
        ListData.Add("Robert");
    }
 }

All logic suggests the latter, but with big data is this the case? 所有逻辑都建议使用后者,但是在大数据的情况下是这样吗?

There is no big difference unless you're adding like millions of entries, so select the code that you're more comfortable with. 除非您要添加数百万个条目,否则没有太大的区别,因此请选择您更喜欢的代码。

Here is a benchmark of 4 different ways. 这是4种不同方式的基准。

1) Array with loop (as in your first method). 1)带循环的数组(与您的第一种方法一样)。

2) Array with AddRange . 2)具有AddRange数组。 Almost identical, but code is a bit nicer. 几乎相同,但是代码要好一些。

3) Using Add (as in your second method). 3)使用Add (与第二种方法一样)。

4) Using list initializer. 4)使用列表初始化器。

Sadly I cannot run each test more than 10,000 times because the dotnetfiddle.net gives a memory error, but you can copy the code and try it on your computer with higher number like 100,000. 遗憾的是,由于dotnetfiddle.net出现内存错误,因此我无法运行每个测试超过10,000次,但是您可以复制代码并在具有更高编号(例如100,000)的计算机上尝试。 Anyway, the result is 1 and 2 are almost identical and faster than 3 and 4 which are very close. 无论如何,结果是1和2几乎相同,并且比非常接近的3和4更快。 But you can see clearly how little the differences are even at 10,000 times of inserting 3 items. 但是您可以清楚地看到,即使在插入3个项目的10,000次后,差异也很小。 So it's really negligible. 因此,这实际上可以忽略不计。

Link to fiddle demo 链接到小提琴演示

Test Results (in milliseconds): 测试结果(以毫秒为单位):

Test1: 6 测试1:6

Test2: 6 测试2:6

Test3: 2 测试3:2

Test4: 2 测试4:2

Test code: 测试代码:

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var c = new CollectionClass();
        var watch = System.Diagnostics.Stopwatch.StartNew();
        for (var i = 0; i < 10000; i++)
            c.Test1();
        watch.Stop();
        Console.WriteLine("Test1:" + watch.ElapsedMilliseconds);

        watch = System.Diagnostics.Stopwatch.StartNew();
        for (var i = 0; i < 10000; i++)
            c.Test2();
        watch.Stop();
        Console.WriteLine("Test2:" + watch.ElapsedMilliseconds);

        watch = System.Diagnostics.Stopwatch.StartNew();
        for (var i = 0; i < 10000; i++)
            c.Test3();
        watch.Stop();
        Console.WriteLine("Test3:" + watch.ElapsedMilliseconds);

        watch = System.Diagnostics.Stopwatch.StartNew();
        for (var i = 0; i < 10000; i++)
            c.Test4();
        watch.Stop();
        Console.WriteLine("Test4:" + watch.ElapsedMilliseconds);
    }
}

internal class CollectionClass
{
    public List<string> ListData { get; set; }

    public void Test1()
    {
        ListData = new List<string>();
        string[] names = { "Matt", "Joanne", "Robert" };

        foreach (var item in names)
            ListData.Add(item);
    }

    public void Test2()
    {
        string[] names = { "Matt", "Joanne", "Robert" };
        ListData = new List<string>();
        ListData.AddRange(names);
    }

    public void Test3()
    {
        ListData = new List<string>();
        ListData.Add("Matt");
        ListData.Add("Joanne");
        ListData.Add("Robert");
    }

    public void Test4()
    {
        ListData = new List<string>(){ "Matt", "Joanne", "Robert" };
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM