简体   繁体   中英

Why is AddRange faster than using a foreach loop?

var fillData = new List<int>();
for (var i = 0; i < 100000; i++)
     fillData.Add(i);

var stopwatch1 = new Stopwatch();
stopwatch1.Start();

var autoFill = new List<int>();
autoFill.AddRange(fillData);
stopwatch1.Stop();

var stopwatch2 = new Stopwatch();
stopwatch2.Start();

var manualFill = new List<int>();

foreach (var i in fillData)
    manualFill.Add(i);
stopwatch2.Stop();

When I take 4 results from stopwach1 and stopwach2 , stopwatch1 has always lower value than stopwatch2 . That means addrange is always faster than foreach . Does anyone know why?

Potentially, AddRange can check where the value passed to it implements IList or IList<T> . If it does, it can find out how many values are in the range, and thus how much space it needs to allocate... whereas the foreach loop may need to reallocate several times.

Additionally, even after allocation, List<T> can use IList<T>.CopyTo to perform a bulk copy into the underlying array (for ranges which implement IList<T> , of course.)

I suspect you'll find that if you try your test again but using Enumerable.Range(0, 100000) for fillData instead of a List<T> , the two will take about the same time.

If you are using Add , it is resizing the inner array gradually as needed (doubling), from the default starting size of 10 (IIRC). If you use:

var manualFill = new List<int>(fillData.Count);

I expect it'll change radically (no more resizes / data copy).

From reflector, AddRange does this internally, rather than growing in doubling:

ICollection<T> is2 = collection as ICollection<T>;
if (is2 != null)
{
    int count = is2.Count;
    if (count > 0)
    {
        this.EnsureCapacity(this._size + count);
        // ^^^ this the key bit, and prevents slow growth when possible ^^^

因为AddRange检查添加项的大小并只增加一次内部数组的大小。

The dissassembly from reflector for the List AddRange method has the following code

ICollection<T> is2 = collection as ICollection<T>;
if (is2 != null)
{
    int count = is2.Count;
    if (count > 0)
    {
        this.EnsureCapacity(this._size + count);
        if (index < this._size)
        {
            Array.Copy(this._items, index, this._items, index + count, this._size - index);
        }
        if (this == is2)
        {
            Array.Copy(this._items, 0, this._items, index, index);
            Array.Copy(this._items, (int) (index + count), this._items, (int) (index * 2), (int) (this._size - index));
        }
        else
        {
            T[] array = new T[count];
            is2.CopyTo(array, 0);
            array.CopyTo(this._items, index);
        }
        this._size += count;
    }
}

As you can see there are some optimizations like EnsureCapacity() call and using Array.Copy().

When using AddRange the Collection can increase the size of the array once and then copy the values into it.

Using a foreach statement the collection needs to increase size of the collection more than once.

Increasing thr size means copying the complete array which takes time.

This is like asking the waiter to bring you one beer ten times and asking him to bring you 10 beers at once.

What do you think is faster :)

i suppose this is the result of optimisation of memory allocation. for AddRange memory allocates only once, and while foreach on each iteration reallocation is done.

also may be there are some optimisations in AddRange implementation (memcpy for example)

在手动添加项目之前尝试初始化初始列表容量:

var manualFill = new List<int>(fillData.Count); 

It is because the Foreach loop will add all the values that the loop is getting one a time and
the AddRange() method will gather all the values it is getting as a "chunk" and add that chunk at once to the specified location.

Simply understanding, it is just like you have a list of 10 items to bring from the market, which would be faster bringing all that one by one or all at single time.

The AddRange extension does not iterate through each item, but applies each item as a whole. Both foreach and .AddRange have a purpose. Addrange will win the contest of speed for your current situation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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