Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Threading;
- namespace BenchmarkLab {
- class TimeMeasure : Benchmark {
- private static double initTime = getInitTime();
- private static double getInitTime() {
- Stopwatch sw = new Stopwatch();
- sw.Start();
- int x = 1;
- for (int i = 0; i < NumOfIterations; ++i) {
- x = i;
- x = i;
- x = i;
- }
- sw.Stop();
- return sw.Elapsed.TotalMilliseconds;
- }
- private static bool isFirstMeasurement = true;
- public static double minExecTime { get; private set; } = 0;
- public TimeMeasure(Object t, List<Action> testsLs) {
- for(int i = 0; i < testsLs.Count; ++i) {
- //prevent the JIT Compiler from optimizing Fkt calls away
- long seed = Environment.TickCount;
- //use the second Core/Processor for the test
- Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
- //prevent "Normal" Processes from interrupting Threads
- Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
- //prevent "Normal" Threads from interrupting this thread
- Thread.CurrentThread.Priority = ThreadPriority.Highest;
- // warming up
- testsLs[i]();
- // clean up
- GC.Collect();
- GC.WaitForPendingFinalizers();
- GC.Collect();
- Stopwatch sw = new Stopwatch();
- sw.Start();
- testsLs[i]();
- sw.Stop();
- if (isFirstMeasurement == true || sw.Elapsed.TotalMilliseconds - initTime < minExecTime) {
- minExecTime = sw.Elapsed.TotalMilliseconds - initTime;
- isFirstMeasurement = false;
- }
- string operation = string.Empty;
- if (i == 0)
- operation = " +=";
- else if (i == 1)
- operation = " -=";
- else if (i == 2)
- operation = " *=";
- else
- operation = " /=";
- typeResults.Add(new DataRow { dataType = t.GetType().Name + operation, time = sw.Elapsed.TotalMilliseconds - initTime,
- opPerSec = getOpPerSec(sw.Elapsed.TotalMilliseconds - initTime) });
- }
- }
- public List<DataRow> typeResults { get; private set; } = new List<DataRow>();
- private double getOpPerSec(double execTime) {
- return NumOfIterations * 1e6 / execTime;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment