teleias

Testing Different Loops (C#)

May 4th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5.  
  6. /*
  7. Testing different loops against each other.
  8. Measured in ticks.
  9. A tick is 100 nanoseconds.
  10.  
  11. Linq Select         :     239267
  12. Linq Sum            :     241414
  13. Linq ForEach        :      94188
  14. ForEach             :      85663
  15. For                 :     118767
  16. ForWithNoVariables  :     148726
  17. */
  18.  
  19. namespace Test
  20. {
  21.     public class Program
  22.     {
  23.         const int N_STYLES = 6;
  24.         const int N_TESTS = 100;
  25.         const int N_DOUBLES_PER_TEST = 1000000;
  26.         static String[] STYLE_NAMES = { "Linq Select", "Linq Sum", "Linq ForEach", "ForEach", "For", "ForWithNoVariables" };
  27.         public static void Main(string[] args)
  28.         {
  29.             for (int i = 0; i < N_STYLES; i++)
  30.                 times.Add(new List<double>());
  31.  
  32.             for (int i = 0; i < N_TESTS; i++)
  33.                 Test();
  34.  
  35.             for (int i = 0; i < N_STYLES; i++)
  36.                 Console.WriteLine(String.Format("{0,-20}", STYLE_NAMES[i])+": "+String.Format("{0,10}", Math.Round(times[i].Average())));
  37.  
  38.             Console.ReadLine();
  39.         }
  40.         static List<List<double>> times = new List<List<double>>();
  41.         public static void Test()
  42.         {
  43.             Random r = new Random();
  44.             List<double> list = new List<double>();
  45.             for (int i = 0; i < N_DOUBLES_PER_TEST; i++)
  46.                 list.Add(r.NextDouble());
  47.  
  48.             List<DateTime> timers = new List<DateTime>();
  49.  
  50.             timers.Add(DateTime.Now);
  51.  
  52.             LinqSelectStyle(list);
  53.  
  54.             timers.Add(DateTime.Now);
  55.  
  56.             LinqSumStyle(list);
  57.  
  58.             timers.Add(DateTime.Now);
  59.  
  60.             LinqForEachStyle(list);
  61.  
  62.             timers.Add(DateTime.Now);
  63.  
  64.             ForEachStyle(list);
  65.  
  66.             timers.Add(DateTime.Now);
  67.  
  68.             ForStyle(list);
  69.  
  70.             timers.Add(DateTime.Now);
  71.  
  72.             ForStyleNoVariable(list);
  73.  
  74.             timers.Add(DateTime.Now);
  75.  
  76.             for (int i = 0; i < N_STYLES; i++)
  77.             {
  78.                 double t = timers[i + 1].Ticks - timers[i].Ticks;
  79.                 times[i].Add(t);
  80.             }
  81.         }
  82.  
  83.         static void LinqSelectStyle(List<double> list)
  84.         {
  85.             double avg, sum;
  86.             avg = sum = 0;
  87.             avg = list.Average();
  88.             sum = list.Select(v => (v - avg) * (v - avg)).Sum();
  89.         }
  90.  
  91.         static void LinqSumStyle(List<double> list)
  92.         {
  93.             double avg, sum;
  94.             avg = sum = 0;
  95.             avg = list.Average();
  96.             sum = list.Sum(v => (v - avg) * (v - avg));
  97.         }
  98.  
  99.         static void LinqForEachStyle(List<double> list)
  100.         {
  101.             double avg, sum;
  102.             avg = sum = 0;
  103.             list.ForEach(delegate (double v) { avg += v; }); avg /= list.Count;
  104.             list.ForEach(delegate (double v) { sum += (v - avg) * (v - avg); });
  105.         }
  106.  
  107.         static void ForEachStyle(List<double> list)
  108.         {
  109.             double avg, sum;
  110.             avg = sum = 0;
  111.             foreach (double v in list)
  112.                 avg += v;
  113.             avg /= list.Count;
  114.             foreach (double v in list)
  115.                 sum += (v - avg) * (v - avg);
  116.         }
  117.  
  118.         static void ForStyle(List<double> list)
  119.         {
  120.             double avg, sum;
  121.             avg = sum = 0;
  122.             for (int i = 0; i < list.Count; i++)
  123.             {
  124.                 double v = list[i];
  125.                 avg += v;
  126.             }
  127.             avg /= list.Count;
  128.             for (int i = 0; i < list.Count; i++)
  129.             {
  130.                 double v = list[i];
  131.                 sum += (v - avg) * (v - avg);
  132.             }
  133.         }
  134.  
  135.         static void ForStyleNoVariable(List<double> list)
  136.         {
  137.             double avg, sum;
  138.             avg = sum = 0;
  139.             for (int i = 0; i < list.Count; i++)
  140.             {
  141.                 avg += list[i];
  142.             }
  143.             avg /= list.Count;
  144.             for (int i = 0; i < list.Count; i++)
  145.             {
  146.                 sum += (list[i] - avg) * (list[i] - avg);
  147.             }
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment