Anarkin

Iterator Tests

Jan 14th, 2015
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.72 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     public class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             new Program().Run();
  13.         }
  14.  
  15.         private Program()
  16.         {
  17.         }
  18.  
  19.         // Results:
  20.         //  TestYield                                72
  21.         //  TestCustomIterator                       4
  22.         //  TestCustomIteratorOnlyImplicit           94
  23.         //  TestList                                 113
  24.         private void Run()
  25.         {
  26.             var tests = new Tests();
  27.  
  28.             Measure("TestYield", tests.TestYield);
  29.             Measure("TestCustomIterator", tests.TestCustomIterator);
  30.             Measure("TestCustomIteratorOnlyImplicit", tests.TestCustomIteratorOnlyImplicit);
  31.             Measure("TestList", tests.TestList);
  32.  
  33.             Console.ReadKey();
  34.         }
  35.  
  36.         private static void Measure(string title, Action method)
  37.         {
  38.             var sw = Stopwatch.StartNew();
  39.  
  40.             method.Invoke();
  41.  
  42.             sw.Stop();
  43.             Console.WriteLine("{0,-40} {1}", title, sw.ElapsedMilliseconds);
  44.         }
  45.     }
  46.  
  47.     public class Tests
  48.     {
  49.         private const int N = 10 * 1000 * 1000;
  50.  
  51.         private readonly int[] _data;
  52.  
  53.         public Tests()
  54.         {
  55.             _data = new int[N];
  56.  
  57.             for (var i = 0; i < N; i++)
  58.                 _data[i] = 1;
  59.         }
  60.  
  61.         private void TestIterator(IEnumerable<int> iterator)
  62.         {
  63.             var t = 0;
  64.             foreach (var i in iterator)
  65.             {
  66.                 t += i;
  67.             }
  68.             Debug.WriteLine(t);
  69.         }
  70.  
  71.         // Without this overload the compiler cannot make use of our explicitly typed methods
  72.         private void TestIterator(SimpleIntList iterator)
  73.         {
  74.             var t = 0;
  75.             foreach (var i in iterator)
  76.             {
  77.                 t += i;
  78.             }
  79.             Debug.WriteLine(t);
  80.         }
  81.  
  82.         // On the other hand, this one has no use
  83.         //private void TestIterator(SimpleIntListOnlyImplicit iterator)
  84.         //{
  85.         //    var t = 0;
  86.         //    foreach (var i in iterator)
  87.         //    {
  88.         //        t += i;
  89.         //    }
  90.         //    Debug.WriteLine(t);
  91.         //}
  92.  
  93.         public void TestYield()
  94.         {
  95.             TestIterator(GetSimpleEnumerator());
  96.         }
  97.  
  98.         private IEnumerable<int> GetSimpleEnumerator()
  99.         {
  100.             for (var i = 0; i < N; i++)
  101.                 yield return 1;
  102.         }
  103.  
  104.         public void TestCustomIterator()
  105.         {
  106.             TestIterator(new SimpleIntList(_data));
  107.         }
  108.  
  109.         public void TestCustomIteratorOnlyImplicit()
  110.         {
  111.             TestIterator(new SimpleIntListOnlyImplicit(_data));
  112.         }
  113.  
  114.         public void TestList()
  115.         {
  116.             TestIterator(new List<int>(_data));
  117.         }
  118.     }
  119.  
  120.     public class SimpleIntList : IEnumerable<int>
  121.     {
  122.         private readonly int[] _data;
  123.  
  124.         public SimpleIntList(int[] data)
  125.         {
  126.             _data = data;
  127.         }
  128.  
  129.         // Explicitly typed GetEnumerator
  130.         public SimpleIntListEnumerator GetEnumerator()
  131.         {
  132.             return new SimpleIntListEnumerator(_data);
  133.         }
  134.  
  135.         IEnumerator<int> IEnumerable<int>.GetEnumerator()
  136.         {
  137.             return GetEnumerator();
  138.         }
  139.  
  140.         IEnumerator IEnumerable.GetEnumerator()
  141.         {
  142.             return GetEnumerator();
  143.         }
  144.     }
  145.  
  146.     public class SimpleIntListOnlyImplicit : IEnumerable<int>
  147.     {
  148.         private readonly int[] _data;
  149.  
  150.         public SimpleIntListOnlyImplicit(int[] data)
  151.         {
  152.             _data = data;
  153.         }
  154.  
  155.         public IEnumerator<int> GetEnumerator()
  156.         {
  157.             return new SimpleIntListEnumerator(_data);
  158.         }
  159.  
  160.         IEnumerator IEnumerable.GetEnumerator()
  161.         {
  162.             return GetEnumerator();
  163.         }
  164.     }
  165.  
  166.     // Class vs Struct... weird affects
  167.     public struct SimpleIntListEnumerator : IEnumerator<int>
  168.     {
  169.         private readonly int[] _data;
  170.         private int _p;
  171.  
  172.         public SimpleIntListEnumerator(int[] data)
  173.         {
  174.             _p = -1;
  175.             _data = data;
  176.         }
  177.  
  178.         public void Dispose()
  179.         {
  180.         }
  181.  
  182.         public bool MoveNext()
  183.         {
  184.             return ++_p < _data.Length;
  185.         }
  186.  
  187.         public void Reset()
  188.         {
  189.             _p = -1;
  190.         }
  191.  
  192.         public int Current
  193.         {
  194.             get
  195.             {
  196.                 return _data[_p];
  197.             }
  198.         }
  199.  
  200.         object IEnumerator.Current { get { return Current; } }
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment