/// <summary> /// Add a profiler /// </summary> /// <param name="run">Start profiling or not</param> /// <returns>Profiler added to map</returns> public StopwatchEx AddProfiler(bool run) { var profiler = new StopwatchEx(); Columns.Add(profiler); if (run) { profiler.Start(); } return(profiler); }
/// <summary> /// Run a algorithm and log execution /// </summary> /// <param name="classname">Class name</param> /// <param name="method">Method name (sort)</param> /// <param name="intList">List with arrays to sort</param> /// <returns>Report log</returns> public static Report RunOneAlgorithm(string classname, string method, List <int[]> intList) { try { GC.Collect(); Logging.WriteLine("################################"); Logging.WriteLine(string.Format("Algorithm: {0}", classname)); Logging.WriteLine(string.Format("Arrays to test: {0}", intList.Count)); Logging.WriteLine("################################"); Type t = Type.GetType(string.Format("eda12131190311906.{0}", classname)); if (t == null) { return(null); } MethodInfo m = t.GetMethod(method, new[] { typeof(int[]) }); Report report = new Report(classname) { XAxisLabel = "Array number of elements", YAxisLabel = "Execution Time (ms)" }; report.PlotTitles.Add("Sort"); report.PlotTitles.Add("Sort-sorted"); Reports.Add(report); int count = 1; foreach (var intA in intList) { var plotLine = new Report.PlotLine(intA.Length.ToString(CultureInfo.InvariantCulture)); report.PlotLines.Add(plotLine); Logging.WriteLine(string.Format("Array nº{0} with {1} elements", count, intA.Length)); var A = (int[])intA.Clone(); report.Comments.Add(""); report.Comments.Add("Array(" + A.Length + ") " + count + ": " + SystemHelper.ArrayToString(A)); Logging.Write("Sorting"); var methodparams = new object[] { A }; if (ApplicationSettings.Instance.ComputeAverageValueWith <= 1) { var profiler = plotLine.AddProfiler(); m.Invoke(null, methodparams); profiler.Stop(); Logging.WriteLine(string.Format(", Sorted after {0}ms", profiler.ElapsedMilliseconds)); } else { Logging.WriteLine(); Logging.WriteLine(string.Format("------Computing Average for {0} executions------", ApplicationSettings.Instance.ComputeAverageValueWith)); var avgList = new List <StopwatchEx>(ApplicationSettings.Instance.ComputeAverageValueWith); for (int i = 0; i < ApplicationSettings.Instance.ComputeAverageValueWith; i++) { Logging.Write(string.Format("{0}: ", i)); A = (int[])intA.Clone(); methodparams = new object[] { A }; StopwatchEx stopwatch = StopwatchEx.StartNew(); m.Invoke(null, methodparams); stopwatch.Stop(); Logging.WriteLine(string.Format("{0}ms", stopwatch.ElapsedMilliseconds)); avgList.Add(stopwatch); } var averageStopWatch = StopwatchEx.ComputeAverage(avgList, ApplicationSettings.Instance .CutLowerHigherAverageValue); plotLine.AddProfiler(averageStopWatch); Logging.WriteLine(string.Format("-----------Computed Average: {0}ms-----------", averageStopWatch.ElapsedMilliseconds)); } Logging.Write("Sorting sorted array"); report.Comments.Add("Sorted array " + count + ": " + SystemHelper.ArrayToString(A)); if (ApplicationSettings.Instance.ComputeAverageValueWith <= 1) { var profiler1 = plotLine.AddProfiler(); m.Invoke(null, methodparams); profiler1.Stop(); Logging.WriteLine(string.Format(", Sorted after {0}ms", profiler1.ElapsedMilliseconds)); } else { Logging.WriteLine(); Logging.WriteLine(string.Format("------Computing Average for {0} executions------", ApplicationSettings.Instance.ComputeAverageValueWith)); var avgList = new List <StopwatchEx>(ApplicationSettings.Instance.ComputeAverageValueWith); for (int i = 0; i < ApplicationSettings.Instance.ComputeAverageValueWith; i++) { Logging.Write(string.Format("{0}: ", i)); StopwatchEx stopwatch = StopwatchEx.StartNew(); m.Invoke(null, methodparams); stopwatch.Stop(); Logging.WriteLine(string.Format("{0}ms", stopwatch.ElapsedMilliseconds)); avgList.Add(stopwatch); } var averageStopWatch = StopwatchEx.ComputeAverage(avgList, ApplicationSettings.Instance .CutLowerHigherAverageValue); Logging.WriteLine(string.Format("-----------Computed Average: {0}ms-----------", averageStopWatch.ElapsedMilliseconds)); plotLine.AddProfiler(averageStopWatch); } report.Comments.Add("Sorted-sorted array " + count + ": " + SystemHelper.ArrayToString(A)); count++; } Logging.WriteLine("################################"); Logging.WriteLine(string.Format("End of {0} algorithmn", classname)); Logging.WriteLine("################################"); report.WriteToFile(); return(report); } catch (ThreadAbortException) { } catch (Exception e) { MessageBox.Show("Error: " + e.Message); } return(null); }
/// <summary> /// Add a profiler /// </summary> /// <param name="profiler">Profiler to add</param> /// <returns>True if added, otherwise false (Duplicated name)</returns> public bool AddProfiler(StopwatchEx profiler) { Columns.Add(profiler); return(true); }