Advertisement
Maarten1977

Testprogram lamda-expression-to-order-sort-my-list-collecti

Aug 15th, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. namespace PerformanceTestSortProperty {
  8.     class Program {
  9.         static void Main(string[] args) {
  10.             const int WARMINGUPSIZE = 1000;
  11.  
  12.             int testSize;
  13.             if (args.Length > 0 && int.TryParse(args[0], out testSize)) {
  14.                 Console.WriteLine("-------------------------------------------------------------------");
  15.                 Console.WriteLine("Testdate: {0}", DateTime.Now);
  16.                 Console.WriteLine("Testsize: {0}", testSize);
  17.                 Console.WriteLine();
  18.                 RunTest("Warming up", WARMINGUPSIZE);
  19.                 RunTest("Testing", testSize);
  20.             } else {
  21.                 Console.WriteLine("Usage: <exefile> <test-size>");
  22.             }
  23.         }
  24.  
  25.         static void RunTest(string title, int size) {
  26.             Console.WriteLine(title);
  27.             var randomSeed = new Random().Next();
  28.             var data = CreateList<Data1>(randomSeed, size, "with lazy properties");
  29.             var ts1 = Sort(data);
  30.             data = CreateList<Data2>(randomSeed, size, "with eager properties");
  31.             var ts2 = Sort(data);
  32.             Console.WriteLine("..Difference in elapsed times absolute: {0}", ts1 - ts2);
  33.             var differenceRelative = (ts1.TotalSeconds - ts2.TotalSeconds) / ts1.TotalSeconds * 100.0;
  34.             Console.WriteLine("..Difference in elapsed times relative: {0:00.00}%", differenceRelative);
  35.         }
  36.  
  37.         static List<IData> CreateList<T>(int randomSeed, int size, string method) where T : IData, new() {
  38.             Console.WriteLine("..Populating list " + method);
  39.             var list = new List<IData>();
  40.             var random = new Random(randomSeed);
  41.             for (int i = 0; i < size; i++) {
  42.                 var x = random.Next(4);
  43.                 var item = new T {
  44.                     Name = Guid.NewGuid().ToString(),
  45.                     AMPM =
  46.                         x == 0 ? "AM" :
  47.                         x == 1 ? "PM" :
  48.                         x == 2 ? "MIX" : "--"
  49.                 };
  50.                 list.Add(item);
  51.             }
  52.             Console.WriteLine("..First 10 values: {0}", string.Join("/", list.Take(10).Select(x => x.AMPM)));
  53.             return list;
  54.         }
  55.  
  56.         static TimeSpan Sort(List<IData> list) {
  57.             var sw = new Stopwatch();
  58.             sw.Start();
  59.             Console.WriteLine("..Starting test");
  60.             var sortedList = list.OrderBy(x => x.AMPM_SortValue).ToList();
  61.             sw.Stop();
  62.             Console.WriteLine("..Elapsed time: {0}", sw.Elapsed);
  63.  
  64.             return sw.Elapsed;
  65.         }
  66.     }
  67.  
  68.     public interface IData {
  69.         string Name { get; set; }
  70.         string AMPM { get; set; }
  71.         int AMPM_SortValue { get; }
  72.     }
  73.  
  74.     public class Data1 : IData {
  75.         public string Name { get; set; }
  76.         public string AMPM { get; set; }
  77.         public int AMPM_SortValue {
  78.             get {
  79.                 if (AMPM == "AM") return 1;
  80.                 if (AMPM == "PM") return 2;
  81.                 if (AMPM == "MIX") return 3;
  82.                 if (AMPM == "--") return 4;
  83.                 return 9;
  84.             }
  85.         }
  86.     }
  87.  
  88.     public class Data2 : IData {
  89.         public string Name { get; set; }
  90.  
  91.         private string _ampm;
  92.         public string AMPM {
  93.             get { return _ampm; }
  94.             set {
  95.                 _ampm = value;
  96.                 AMPM_SortValue = AppropriateSort();
  97.             }
  98.         }
  99.  
  100.         public int AMPM_SortValue { get; private set; }
  101.  
  102.         private int AppropriateSort() {
  103.             if (AMPM == "AM") return 1;
  104.             if (AMPM == "PM") return 2;
  105.             if (AMPM == "MIX") return 3;
  106.             return AMPM == "--" ? 4 : 9;
  107.         }
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement