Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Diagnostics;
- namespace PerformanceTestSortProperty {
- class Program {
- static void Main(string[] args) {
- const int WARMINGUPSIZE = 1000;
- int testSize;
- if (args.Length > 0 && int.TryParse(args[0], out testSize)) {
- Console.WriteLine("-------------------------------------------------------------------");
- Console.WriteLine("Testdate: {0}", DateTime.Now);
- Console.WriteLine("Testsize: {0}", testSize);
- Console.WriteLine();
- RunTest("Warming up", WARMINGUPSIZE);
- RunTest("Testing", testSize);
- } else {
- Console.WriteLine("Usage: <exefile> <test-size>");
- }
- }
- static void RunTest(string title, int size) {
- Console.WriteLine(title);
- var randomSeed = new Random().Next();
- var data = CreateList<Data1>(randomSeed, size, "with lazy properties");
- var ts1 = Sort(data);
- data = CreateList<Data2>(randomSeed, size, "with eager properties");
- var ts2 = Sort(data);
- Console.WriteLine("..Difference in elapsed times absolute: {0}", ts1 - ts2);
- var differenceRelative = (ts1.TotalSeconds - ts2.TotalSeconds) / ts1.TotalSeconds * 100.0;
- Console.WriteLine("..Difference in elapsed times relative: {0:00.00}%", differenceRelative);
- }
- static List<IData> CreateList<T>(int randomSeed, int size, string method) where T : IData, new() {
- Console.WriteLine("..Populating list " + method);
- var list = new List<IData>();
- var random = new Random(randomSeed);
- for (int i = 0; i < size; i++) {
- var x = random.Next(4);
- var item = new T {
- Name = Guid.NewGuid().ToString(),
- AMPM =
- x == 0 ? "AM" :
- x == 1 ? "PM" :
- x == 2 ? "MIX" : "--"
- };
- list.Add(item);
- }
- Console.WriteLine("..First 10 values: {0}", string.Join("/", list.Take(10).Select(x => x.AMPM)));
- return list;
- }
- static TimeSpan Sort(List<IData> list) {
- var sw = new Stopwatch();
- sw.Start();
- Console.WriteLine("..Starting test");
- var sortedList = list.OrderBy(x => x.AMPM_SortValue).ToList();
- sw.Stop();
- Console.WriteLine("..Elapsed time: {0}", sw.Elapsed);
- return sw.Elapsed;
- }
- }
- public interface IData {
- string Name { get; set; }
- string AMPM { get; set; }
- int AMPM_SortValue { get; }
- }
- public class Data1 : IData {
- public string Name { get; set; }
- public string AMPM { get; set; }
- public int AMPM_SortValue {
- get {
- if (AMPM == "AM") return 1;
- if (AMPM == "PM") return 2;
- if (AMPM == "MIX") return 3;
- if (AMPM == "--") return 4;
- return 9;
- }
- }
- }
- public class Data2 : IData {
- public string Name { get; set; }
- private string _ampm;
- public string AMPM {
- get { return _ampm; }
- set {
- _ampm = value;
- AMPM_SortValue = AppropriateSort();
- }
- }
- public int AMPM_SortValue { get; private set; }
- private int AppropriateSort() {
- if (AMPM == "AM") return 1;
- if (AMPM == "PM") return 2;
- if (AMPM == "MIX") return 3;
- return AMPM == "--" ? 4 : 9;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement