Beispiel #1
0
 static Tuple<string, TimeSpan>[] CheckPerformance<T>(IMethods<T> methods) where T:class
 {
   var stats = new List<Tuple<string, TimeSpan>>();
   T source = null;
   foreach (var info in new[] 
     { 
       new {Name = "Generate", Method = new Func<T, T>(items => methods.Generate(10000000, i => i % 2 == 0 ? -i : i))}, 
       new {Name = "Sum", Method =  new Func<T, T>(items => {Console.WriteLine(methods.Sum(items));return items;})}, 
       new {Name = "BlockCopy", Method = new Func<T, T>(items => methods.BlockCopy(items))}, 
       new {Name = "Sort", Method = new Func<T, T>(items => methods.BlockCopy(items))}, 
       new {Name = "Sum", Method =  new Func<T, T>(items => {Console.WriteLine(methods.Sum(items));return items;})}, 
     }
    )
   {
     int count = 10;
     var stopwatch = new Stopwatch();
     stopwatch.Start();
     T res = null;
     for (var i = 0; i < count; ++i)
       res = info.Method(source);
     stopwatch.Stop();
     source = res;
     stats.Add(new Tuple<string, TimeSpan>(info.Name, stopwatch.Elapsed));
   }
   return stats.ToArray();
 }