Author Topic: Questions Not Worth Their Own Thread: C# Edition  (Read 374676 times)

0 Members and 2 Guests are viewing this topic.

Offline db48x

  • Commodore
  • **********
  • d
  • Posts: 646
  • Thanked: 200 times
Re: Questions Not Worth Their Own Thread: C# Edition
« Reply #4035 on: Today at 06:56:47 PM »
So now you have made me curious, I started reading about it. Some people expressed a view that IEnumerable  only guarantees an enumeration and doesn't guarantee the order of execution (even if ordered beforehand). However, as I read it, its the same execution as list because the latter inherited the former. Which is correct?

Err, both, if I understand the question. An IEnumerable method returns an IEnumerator which has a MoveNext method that returns the next item. Calling that method is what triggers the enumerator to start doing its job. If you use a foreach loop, C# calls MoveNext for you until the enumerator is exhausted.

Each Enumerable method, such as Select or Except or OrderBy, returns a different IEnumerator implementation that stores any state that it needs plus the next IEnumerator along in the chain. For example, Select takes a projection function that maps from one type to another, and uses it to transform all of the elements of the enumeration into the result type. You could implement its IEnumerator approximately like this:

Code: [Select]
public class SelectEnumerator : IEnumerator
{
    private IEnumerator _inner;
    private Func<TSource, i32, TResult> _func;
    private TResult _current;

    public bool MoveNext()
    {
        _current = null;
        return _inner.MoveNext();
    }

    object IEnumerator.Current
    {
        get
        {
            if (_current) return _current;
            return _current = _func(_inner.Current);
        }
    }
}

The Where method is similar, but it only returns elements where the predicate returns true. You could write it approximately like this:

Code: [Select]
public class WhereEnumerator : IEnumerator
{
    private IEnumerator _inner;
    private Func<TSource, Boolean> _func;

    public bool MoveNext()
    {
        foreach (var e in _inner) {
            if (_func(e)) {
                return true;
            }
        }
        return false;
    }

    object IEnumerator.Current
    {
        get
        {
            return _inner.Current;
        }
    }
}

So to answer your question, the various Enumerable methods that you call only create these IEnumerator objects, and each Enumerator only runs enough code to get you the next item along. The execution of all of the Enumerators is interleaved, since each one calls the next one in the chain at least once. With some data sources the order of the results might vary, since data might come in from a database in an arbitrary order (for example), but since you are starting with a List then that will never happen. The elements returned by the enumerator will always be in the same order as they were in the original list, at least until you sort them.


EDIT

I just ran an experiment for the missile salvo list on the fleet window, which I just updated.

[…]

I ran both functions several times and in every case, the List version is faster than the IEnumerable version. It may be that the IEnumerable is faster to create, but the List seems to be faster to use. Maybe because everything is preloaded into memory for the List, whereas the IEnumerable has to retrieve the information as needed.

Of course I would need to see all of the code, and to know how you measured the performance to be able to say why that might be. See below…

Further reading suggests that every time you access a method or property of the same element within an IEnumerable foreach, it has to retrieve it again. A list already has it in memory.

Note quite. Notice that IEnumerator has a property called Current that holds the current value. They are all written so that they don't do any extra work if you retrieve that value more than once; you can see how I did that in SelectEnumerator above. And anyway, since you use a foreach loop C# is assigning that value to a local variable anyway. It doesn’t have to go back to the enumerator at all even if you access multiple properties or methods on the value that local variable holds.

So its likely that IEnumerable  is faster if you are doing very little with the data you enumerate, but List is faster if you want to access each element more than once (either in multiple iterations, or getting using multiple properties/methods for each element of a single iteration).

This is definitely true. If you need to multiple values from the enumeration at once, for example the nth − 1 element as well as the nth, then you need to write it in such a way that the enumerator doesn't have to be reset back to the start and work it’s way back down to the previous element. It should be fairly hard to make that mistake just by using foreach loops though.