Advertisement
pszczyg

EnumSpeed_05

Sep 15th, 2021
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.84 KB | None | 0 0
  1.   public class Clock
  2.     {
  3.         interface IStopwatch
  4.         {
  5.             bool IsRunning { get; }
  6.             TimeSpan Elapsed { get; }
  7.  
  8.             void Start();
  9.             void Stop();
  10.             void Reset();
  11.         }
  12.  
  13.         class TimeWatch : IStopwatch
  14.         {
  15.             Stopwatch stopwatch = new Stopwatch();
  16.  
  17.             public TimeSpan Elapsed
  18.             {
  19.                 get { return stopwatch.Elapsed; }
  20.             }
  21.  
  22.             public bool IsRunning
  23.             {
  24.                 get { return stopwatch.IsRunning; }
  25.             }
  26.  
  27.  
  28.  
  29.             public TimeWatch()
  30.             {
  31.                 if (!Stopwatch.IsHighResolution)
  32.                     throw new NotSupportedException("Your hardware doesn't support high resolution counter");
  33.  
  34.                 //prevent the JIT Compiler from optimizing Fkt calls away
  35.                 long seed = Environment.TickCount;
  36.  
  37.                 //use the second Core/Processor for the test
  38.                 Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
  39.  
  40.                 //prevent "Normal" Processes from interrupting Threads
  41.                 Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
  42.  
  43.                 //prevent "Normal" Threads from interrupting this thread
  44.                 Thread.CurrentThread.Priority = ThreadPriority.Highest;
  45.             }
  46.  
  47.  
  48.  
  49.             public void Start()
  50.             {
  51.                 stopwatch.Start();
  52.             }
  53.  
  54.             public void Stop()
  55.             {
  56.                 stopwatch.Stop();
  57.             }
  58.  
  59.             public void Reset()
  60.             {
  61.                 stopwatch.Reset();
  62.             }
  63.         }
  64.  
  65.         class CpuWatch : IStopwatch
  66.         {
  67.             TimeSpan startTime;
  68.             TimeSpan endTime;
  69.             bool isRunning;
  70.  
  71.  
  72.  
  73.             public TimeSpan Elapsed
  74.             {
  75.                 get
  76.                 {
  77.                     if (IsRunning)
  78.                         throw new NotImplementedException("Getting elapsed span while watch is running is not implemented");
  79.  
  80.                     return endTime - startTime;
  81.                 }
  82.             }
  83.  
  84.             public bool IsRunning
  85.             {
  86.                 get { return isRunning; }
  87.             }
  88.  
  89.  
  90.  
  91.             public void Start()
  92.             {
  93.                 startTime = Process.GetCurrentProcess().TotalProcessorTime;
  94.                 isRunning = true;
  95.             }
  96.  
  97.             public void Stop()
  98.             {
  99.                 endTime = Process.GetCurrentProcess().TotalProcessorTime;
  100.                 isRunning = false;
  101.             }
  102.  
  103.             public void Reset()
  104.             {
  105.                 startTime = TimeSpan.Zero;
  106.                 endTime = TimeSpan.Zero;
  107.             }
  108.         }
  109.  
  110.         public static void BenchmarkTime(Action<int> action, int iterations = 10000)
  111.         {
  112.             Benchmark<TimeWatch>(action, iterations);
  113.         }
  114.  
  115.         static void Benchmark<T>(Action<int> action, int iterations) where T : IStopwatch, new()
  116.         {
  117.             //clean Garbage
  118.             GC.Collect();
  119.  
  120.             //wait for the finalizer queue to empty
  121.             GC.WaitForPendingFinalizers();
  122.  
  123.             //clean Garbage
  124.             GC.Collect();
  125.  
  126.             //warm up
  127.             action(0);
  128.  
  129.             var stopwatch = new T();
  130.             var timings = new double[5];
  131.             for (int i = 0; i < timings.Length; i++)
  132.             {
  133.                 stopwatch.Reset();
  134.                 stopwatch.Start();
  135.                 for (int j = 0; j < iterations; j++)
  136.                     action(j);
  137.                 stopwatch.Stop();
  138.                 timings[i] = stopwatch.Elapsed.TotalMilliseconds;
  139.                 Console.WriteLine(timings[i]);
  140.             }
  141.             Console.WriteLine("normalized mean: " + timings.NormalizedMean().ToString());
  142.         }
  143.  
  144.         public static void BenchmarkCpu(Action<int> action, int iterations = 10000)
  145.         {
  146.             Benchmark<CpuWatch>(action, iterations);
  147.         }
  148.     }
  149.  
  150.     public static class PerformanceMeasurementExtensionMethods
  151.     {
  152.         public static double NormalizedMean(this ICollection<double> values)
  153.         {
  154.             if (values.Count == 0)
  155.                 return double.NaN;
  156.  
  157.             var deviations = values.Deviations().ToArray();
  158.             var meanDeviation = deviations.Sum(t => Math.Abs(t.Item2)) / values.Count;
  159.             return deviations.Where(t => t.Item2 > 0 || Math.Abs(t.Item2) <= meanDeviation).Average(t => t.Item1);
  160.         }
  161.  
  162.         public static IEnumerable<Tuple<double, double>> Deviations(this ICollection<double> values)
  163.         {
  164.             if (values.Count == 0)
  165.                 yield break;
  166.  
  167.             var avg = values.Average();
  168.             foreach (var d in values)
  169.                 yield return Tuple.Create(d, avg - d);
  170.         }
  171.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement