Why is Enumerable.Range faster than a direct yield loop?

后端 未结 4 1726
离开以前
离开以前 2020-12-09 12:07

The code below is checking performance of three different ways to do same solution.

    public static void Main(string[] args)
    {
        // for loop
             


        
4条回答
  •  一整个雨季
    2020-12-09 12:35

    Why should Enumerable.Range be any slower than your self-made GetIntRange? In fact, if Enumerable.Range were defined as

    public static class Enumerable {
        public static IEnumerable Range(int start, int count) {
            var end = start + count;
            for(var current = start; current < end; ++current) {
                yield return current;
            }
        }
    }
    

    then it should be exactly as fast as your self-made GetIntRange. This is in fact the reference implementation for Enumerable.Range, absent any tricks on the part of the compiler or programmer.

    You may want to compare your GetIntRange and System.Linq.Enumerable.Range with the following implementation (of course, compile in release mode, as Rob points out). This implementation may be slightly optimized with respect to what a compiler would generate from an iterator block.

    public static class Enumerable {
        public static IEnumerable Range(int start, int count) {
            return new RangeEnumerable(start, count);
        }
        private class RangeEnumerable : IEnumerable {
            private int _Start;
            private int _Count;
            public RangeEnumerable(int start, int count) {
                _Start = start;
                _Count = count;
            }
            public virtual IEnumerator GetEnumerator() {
                return new RangeEnumerator(_Start, _Count);
            }
            IEnumerator IEnumerable.GetEnumerator() {
                return GetEnumerator();
            }
        }
        private class RangeEnumerator : IEnumerator {
            private int _Current;
            private int _End;
            public RangeEnumerator(int start, int count) {
                _Current = start - 1;
                _End = start + count;
            }
            public virtual void Dispose() {
                _Current = _End;
            }
            public virtual void Reset() {
                throw new NotImplementedException();
            }
            public virtual bool MoveNext() {
                ++_Current;
                return _Current < _End;
            }
            public virtual int Current { get { return _Current; } }
            object IEnumerator.Current { get { return Current; } }
        }
    }
    

提交回复
热议问题