olexander_dan

Untitled

Sep 23rd, 2019
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Threading;
  5.  
  6. namespace BenchmarkLab {
  7.     class TimeMeasure : Benchmark {
  8.         private static double initTime = getInitTime();
  9.         private static double getInitTime() {
  10.             Stopwatch sw = new Stopwatch();
  11.             sw.Start();
  12.                 int x = 1;
  13.                 for (int i = 0; i < NumOfIterations; ++i) {
  14.                     x = i;
  15.                     x = i;
  16.                     x = i;
  17.                 }
  18.             sw.Stop();
  19.             return sw.Elapsed.TotalMilliseconds;
  20.         }
  21.  
  22.         private static bool isFirstMeasurement = true;
  23.         public static double minExecTime { get; private set; } = 0;
  24.  
  25.         public TimeMeasure(Object t, List<Action> testsLs) {
  26.             for(int i = 0; i < testsLs.Count; ++i) {
  27.                 //prevent the JIT Compiler from optimizing Fkt calls away
  28.                 long seed = Environment.TickCount;
  29.  
  30.                 //use the second Core/Processor for the test
  31.                 Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
  32.  
  33.                 //prevent "Normal" Processes from interrupting Threads
  34.                 Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
  35.  
  36.                 //prevent "Normal" Threads from interrupting this thread
  37.                 Thread.CurrentThread.Priority = ThreadPriority.Highest;
  38.  
  39.                 // warming up
  40.                 testsLs[i]();
  41.  
  42.                 // clean up
  43.                 GC.Collect();
  44.                 GC.WaitForPendingFinalizers();
  45.                 GC.Collect();
  46.  
  47.                 Stopwatch sw = new Stopwatch();
  48.                 sw.Start();
  49.                     testsLs[i]();
  50.                 sw.Stop();
  51.  
  52.                 if (isFirstMeasurement == true || sw.Elapsed.TotalMilliseconds - initTime < minExecTime) {
  53.                     minExecTime = sw.Elapsed.TotalMilliseconds - initTime;
  54.                     isFirstMeasurement = false;
  55.                 }
  56.  
  57.                 string operation = string.Empty;
  58.                 if (i == 0)
  59.                     operation = " +=";
  60.                 else if (i == 1)
  61.                     operation = " -=";
  62.                 else if (i == 2)
  63.                     operation = " *=";
  64.                 else
  65.                     operation = " /=";
  66.  
  67.                 typeResults.Add(new DataRow { dataType = t.GetType().Name + operation, time = sw.Elapsed.TotalMilliseconds - initTime,
  68.                                               opPerSec = getOpPerSec(sw.Elapsed.TotalMilliseconds - initTime) });
  69.             }
  70.         }
  71.  
  72.         public List<DataRow> typeResults { get; private set; } = new List<DataRow>();
  73.        
  74.         private double getOpPerSec(double execTime) {
  75.             return NumOfIterations * 1e6 / execTime;
  76.         }
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment