Advertisement
DigitalMag

lazy ienumerables benchmarks

Jun 13th, 2020
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace GreedyLinq
  9. {
  10.     public class Man
  11.     {
  12.         public int Age { get; set; } = 30;
  13.         public Man ChangeAge(int age)
  14.         {
  15.             Age = age;
  16.             return this;
  17.         }
  18.     }
  19.  
  20.     public class Test
  21.     {
  22.         Dictionary<Type, List<long[]>> results = new Dictionary<Type, List<long[]>>();
  23.  
  24.         public void PrintResults()
  25.         {
  26.  
  27.  
  28.  
  29.  
  30.             foreach (var type in results.Keys)
  31.             {
  32.                 var bench = results[type];
  33.  
  34.                 Console.WriteLine("Results for " + type.Name + ":" + Environment.NewLine + Environment.NewLine);
  35.                 Console.WriteLine("Jit compiled time: ");
  36.  
  37.                 Console.WriteLine("Linq: " + results[type].First()[0] + " ms");
  38.                 Console.WriteLine("Linq: " + results[type].First()[1] + " ticks");
  39.                 Console.WriteLine("Calculate: " + results[type].First()[2] + " ms");
  40.                 Console.WriteLine("Calculate: " + results[type].First()[3] + " ticks" + Environment.NewLine);
  41.  
  42.                 var avg = new long[4] { 0, 0, 0, 0 };                
  43.  
  44.                 for (int j = 1; j < results[type].Count; j++)
  45.                 {
  46.                     avg[0] += results[type][j][0];
  47.                     avg[1] += results[type][j][1];
  48.                     avg[2] += results[type][j][2];
  49.                     avg[3] += results[type][j][3];
  50.                 }
  51.  
  52.                 Console.WriteLine("Avg after Jit time: ");
  53.                 Console.WriteLine("Linq: " + avg[0]/(avg.Length - 1) + " ms");
  54.                 Console.WriteLine("Linq: " + avg[1] / (avg.Length - 1) + " ticks");
  55.                 Console.WriteLine("Calculate: " + avg[2] / (avg.Length - 1) + " ms");
  56.                 Console.WriteLine("Calculate: " + avg[3] / (avg.Length - 1) + " ticks" + Environment.NewLine);
  57.                 Console.WriteLine(); Console.WriteLine();
  58.             }
  59.         }                
  60.  
  61.         public T[] LinqStart<T>(IEnumerable<T> source)
  62.         {
  63.  
  64.             var sw = new Stopwatch(); sw.Start();
  65.  
  66.             IEnumerable<T> middleRezs = null;
  67.             if (source is IEnumerable<Man> src) middleRezs = ManTest(src) as IEnumerable<T>;
  68.             else if (source is IEnumerable<int> isrc) middleRezs = IntTest(isrc) as IEnumerable<T>;
  69.             else return null;
  70.  
  71.             var lms = sw.ElapsedMilliseconds; var lticks = sw.ElapsedTicks;// here
  72.  
  73.             var arr = middleRezs.ToArray(); sw.Stop();
  74.  
  75.             var ams = sw.ElapsedMilliseconds - lms;
  76.             var aticks = sw.ElapsedTicks - lticks;
  77.             var rez = new long[] { lms, lticks, ams, aticks };
  78.             if (results.ContainsKey(typeof(T))) results[typeof(T)].Add(rez);
  79.             else
  80.                 results.Add(typeof(T), new List<long[]> { rez });
  81.  
  82.             return arr;
  83.         }
  84.  
  85.         private static IEnumerable<Man> ManTest(IEnumerable<Man> source)
  86.         {
  87.             return source
  88.                 .Select(m => new Man { Age = m.Age + 1 })
  89.                 .Select(m => new Man { Age = m.Age * 2 })
  90.                 .Where(c => c.Age % 2 == 0);
  91.         }
  92.  
  93.         private static IEnumerable<int> IntTest(IEnumerable<int> source)
  94.         {
  95.             return source.Select(c => c + 1).Select(c => c * 2).Where(c => c % 2 == 0);
  96.             // here
  97.         }
  98.     }
  99.  
  100.     class Program
  101.     {
  102.  
  103.         static void Main(string[] args)
  104.         {
  105.             Console.Write("press either key for the test");
  106.             Console.Read();
  107.  
  108.             var test = new Test();
  109.  
  110.             List<Man> manList = new List<Man>();
  111.             List<int> intList = new List<int>();
  112.             for (int i = 0; i < 100; i++)
  113.             {
  114.                 manList.Add(new Man { Age = i + 15 });
  115.                 intList.Add(i+15);
  116.             }
  117.  
  118.             for (int i = 0; i < 5; i++) { test.LinqStart(manList); Console.WriteLine(i + " test man performed"); }
  119.             for (int i = 0; i < 5; i++) { test.LinqStart(intList); Console.WriteLine(i + " test int performed"); }
  120.  
  121.             Console.Write("press either key for the results");
  122.             Console.ReadKey();
  123.             Console.WriteLine();
  124.             Console.WriteLine();
  125.  
  126.             test.PrintResults();
  127.  
  128.             Console.ReadKey();
  129.         }
  130.     }
  131. }
  132.  
  133.  
  134. /*
  135.  
  136. Results for Man:
  137.  
  138.  
  139. Jit compiled time:
  140. Linq: 1 ms
  141. Linq: 3484 ticks
  142. Calculate: 1 ms
  143. Calculate: 2148 tick
  144.  
  145. Avg after Jit time:
  146. Linq: 0 ms
  147. Linq: 21 ticks
  148. Calculate: 0 ms
  149. Calculate: 106 ticks
  150.  
  151.  
  152.  
  153. Results for Int32:
  154.  
  155.  
  156. Jit compiled time:
  157. Linq: 10 ms
  158. Linq: 23855 ticks
  159. Calculate: 1 ms
  160. Calculate: 3679 tick
  161.  
  162. Avg after Jit time:
  163. Linq: 0 ms
  164. Linq: 20 ticks
  165. Calculate: 0 ms
  166. Calculate: 44 ticks
  167.  
  168. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement