Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main()
- {
- new Program().Run();
- }
- private Program()
- {
- }
- // Results:
- // TestYield 72
- // TestCustomIterator 4
- // TestCustomIteratorOnlyImplicit 94
- // TestList 113
- private void Run()
- {
- var tests = new Tests();
- Measure("TestYield", tests.TestYield);
- Measure("TestCustomIterator", tests.TestCustomIterator);
- Measure("TestCustomIteratorOnlyImplicit", tests.TestCustomIteratorOnlyImplicit);
- Measure("TestList", tests.TestList);
- Console.ReadKey();
- }
- private static void Measure(string title, Action method)
- {
- var sw = Stopwatch.StartNew();
- method.Invoke();
- sw.Stop();
- Console.WriteLine("{0,-40} {1}", title, sw.ElapsedMilliseconds);
- }
- }
- public class Tests
- {
- private const int N = 10 * 1000 * 1000;
- private readonly int[] _data;
- public Tests()
- {
- _data = new int[N];
- for (var i = 0; i < N; i++)
- _data[i] = 1;
- }
- private void TestIterator(IEnumerable<int> iterator)
- {
- var t = 0;
- foreach (var i in iterator)
- {
- t += i;
- }
- Debug.WriteLine(t);
- }
- // Without this overload the compiler cannot make use of our explicitly typed methods
- private void TestIterator(SimpleIntList iterator)
- {
- var t = 0;
- foreach (var i in iterator)
- {
- t += i;
- }
- Debug.WriteLine(t);
- }
- // On the other hand, this one has no use
- //private void TestIterator(SimpleIntListOnlyImplicit iterator)
- //{
- // var t = 0;
- // foreach (var i in iterator)
- // {
- // t += i;
- // }
- // Debug.WriteLine(t);
- //}
- public void TestYield()
- {
- TestIterator(GetSimpleEnumerator());
- }
- private IEnumerable<int> GetSimpleEnumerator()
- {
- for (var i = 0; i < N; i++)
- yield return 1;
- }
- public void TestCustomIterator()
- {
- TestIterator(new SimpleIntList(_data));
- }
- public void TestCustomIteratorOnlyImplicit()
- {
- TestIterator(new SimpleIntListOnlyImplicit(_data));
- }
- public void TestList()
- {
- TestIterator(new List<int>(_data));
- }
- }
- public class SimpleIntList : IEnumerable<int>
- {
- private readonly int[] _data;
- public SimpleIntList(int[] data)
- {
- _data = data;
- }
- // Explicitly typed GetEnumerator
- public SimpleIntListEnumerator GetEnumerator()
- {
- return new SimpleIntListEnumerator(_data);
- }
- IEnumerator<int> IEnumerable<int>.GetEnumerator()
- {
- return GetEnumerator();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- public class SimpleIntListOnlyImplicit : IEnumerable<int>
- {
- private readonly int[] _data;
- public SimpleIntListOnlyImplicit(int[] data)
- {
- _data = data;
- }
- public IEnumerator<int> GetEnumerator()
- {
- return new SimpleIntListEnumerator(_data);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- }
- // Class vs Struct... weird affects
- public struct SimpleIntListEnumerator : IEnumerator<int>
- {
- private readonly int[] _data;
- private int _p;
- public SimpleIntListEnumerator(int[] data)
- {
- _p = -1;
- _data = data;
- }
- public void Dispose()
- {
- }
- public bool MoveNext()
- {
- return ++_p < _data.Length;
- }
- public void Reset()
- {
- _p = -1;
- }
- public int Current
- {
- get
- {
- return _data[_p];
- }
- }
- object IEnumerator.Current { get { return Current; } }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment