Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. sealed class Tester : Tester<object>
  2.     {
  3.         private readonly Action action;
  4.  
  5.         public Tester(Action action, int times)
  6.         {
  7.             this.action = action;
  8.             this.times = times;
  9.         }
  10.  
  11.         new public async Task<long> Run()
  12.         {
  13.             var stopWatch = new Stopwatch();
  14.             long timeSpent = 0;
  15.  
  16.             await Task.Run(() =>
  17.             {
  18.                 for (int i = 0; i < times; i++)
  19.                 {
  20.                     stopWatch.Start();
  21.                     action();
  22.                     stopWatch.Stop();
  23.  
  24.                     timeSpent += stopWatch.ElapsedMilliseconds;
  25.                 }
  26.             });
  27.  
  28.             return timeSpent / times;
  29.         }
  30.     }
  31.  
  32.     class Tester<T>
  33.     {
  34.         private readonly Action<T> action;
  35.         private readonly Func<T> seed;
  36.         private readonly bool useSeed;
  37.         protected int times;
  38.  
  39.         protected Tester() { }
  40.  
  41.         public Tester(Action<T> action, int times, Func<T> seed)
  42.         {
  43.             this.action = action;
  44.             this.times = times;
  45.             this.seed = seed;
  46.             useSeed = true;
  47.         }
  48.  
  49.         public async Task<long> Run()
  50.         {
  51.             var stopWatch = new Stopwatch();
  52.             long timeSpent = 0;
  53.  
  54.             await Task.Run(() =>
  55.             {
  56.                 for (int i = 0; i < times; i++)
  57.                 {
  58.                     var seedValue = seed();
  59.                     stopWatch.Start();
  60.                     action(seedValue);
  61.                     stopWatch.Stop();
  62.  
  63.                     timeSpent += stopWatch.ElapsedMilliseconds;
  64.                 }
  65.             });
  66.  
  67.             return timeSpent/times;
  68.         }
  69.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement