daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Dec 11th, 2018 156 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Profiling
  8. {
  9.     class Generator
  10.     {
  11.         static readonly string braceOpen = "\n{\n";
  12.         static readonly string braceClose = "\n}";
  13.         public static string GenerateDeclarations()
  14.         {
  15.             var builder = new StringBuilder();
  16.             var headers = new string[2] { "struct S", "class C" };
  17.             StringBuilder fields;
  18.             foreach (var i in Constants.FieldCounts)
  19.                 foreach (var header in headers)
  20.                 {
  21.                     fields = new StringBuilder();
  22.                     for (var j = 0; j < i; j++)
  23.                         fields.AppendFormat("byte Value{0}; ", j);
  24.                     builder.AppendFormat("\n{0}{1}{2}{3}", header + i, braceOpen, fields.ToString(), braceClose);
  25.                 }
  26.             return builder.ToString();
  27.         }
  28.  
  29.         static string GetMethodBlock(bool isClass, int index)
  30.         {
  31.             var type = GetType(isClass, index);
  32.             var header = "void P" + type +"()";
  33.             var methodBody = string.Format("var array = new {0}[Constants.ArraySize];", type);
  34.             if (isClass)
  35.             {
  36.                 var initialize = "for (int i = 0; i < Constants.ArraySize; i++) array[i] = new C{0}(); ";
  37.                 methodBody += string.Format(initialize, index);
  38.             }
  39.             return "\n" + header + braceOpen + methodBody + braceClose;
  40.         }
  41.  
  42.         static string GetType(bool isClass, int index)
  43.         {
  44.             return (isClass ? "C" : "S") + index;
  45.         }
  46.  
  47.         static string GetCallBlock(bool isClass, int index)
  48.         {
  49.             var inversion = isClass ? "" : "!";
  50.             var type = GetType(isClass, index);
  51.             var body =
  52.             @"
  53.            if ({0}isClass && size == {1})
  54.            {2}
  55.                for (int i = 0; i < count; i++) P{3}();
  56.                return;
  57.            {4}
  58.            ";
  59.             return string.Format(body, inversion, index, braceOpen, type, braceClose);
  60.         }
  61.         static string GetCallMethod()
  62.         {
  63.             var header = "public void Call(bool isClass, int size, int count)";
  64.             var calls = MakeForEachI(GetCallBlock, true);
  65.             var exceptor = "throw new ArgumentException();";
  66.             return header + braceOpen + calls + exceptor + braceClose;
  67.         }
  68.  
  69.         static string MakeForEachI(Func<bool, int, string> func, bool isOrderDirect)
  70.         {
  71.             var bools = isOrderDirect ? new[] { true, false } : new[] { false, true };
  72.             var builder = new StringBuilder();
  73.             foreach (var i in Constants.FieldCounts)
  74.             {
  75.                 builder.Append(func(bools[0], i));
  76.                 builder.Append(func(bools[1], i));
  77.             }
  78.             return builder.ToString();
  79.         }
  80.         public static string GenerateArrayRunner()
  81.         {
  82.             var header = "public class ArrayRunner : IRunner";
  83.             var methods = MakeForEachI(GetMethodBlock, true);
  84.             var callMethod = GetCallMethod();
  85.             return header + braceOpen + methods + callMethod + braceClose;
  86.         }
  87.         public static string GenerateCallRunner()
  88.         {
  89.             throw new NotImplementedException();
  90.         }
  91.     }
  92. }
  93.  
  94.  
  95. using System;
  96. using System.Collections.Generic;
  97. using System.Diagnostics;
  98.  
  99. namespace Profiling
  100. {
  101.     public class ProfilerTask : IProfiler
  102.     {
  103.         public void Preheat(int size, IRunner runner)
  104.         {
  105.             runner.Call(true, size, 1);
  106.             runner.Call(false, size, 1);
  107.         }
  108.  
  109.         public List<ExperimentResult> Measure(IRunner runner, int repetitionsCount)
  110.         {
  111.             Stopwatch watch = new Stopwatch();
  112.             var results = new List<ExperimentResult>();
  113.             var bools = new[] { true, false };
  114.             var elapsedTime = new double[2];
  115.             foreach (var size in Constants.FieldCounts)
  116.             {
  117.                 Preheat(size, runner);
  118.                 for(var i = 0; i < 2; i++)
  119.                 {
  120.                     watch.Reset();
  121.                     watch.Start();
  122.                     runner.Call(bools[i], size, repetitionsCount);
  123.                     watch.Stop();
  124.                     elapsedTime[i] = watch.ElapsedMilliseconds / (double)repetitionsCount;
  125.                 }
  126.                 results.Add(new ExperimentResult(size, elapsedTime[0], elapsedTime[1]));
  127.             }
  128.             return results;
  129.         }
  130.     }
  131. }
  132.  
  133.  
  134.  
  135.  
  136.  
  137. using System.Collections.Generic;
  138. using System.Drawing;
  139. using System.Linq;
  140. using System.Windows.Forms;
  141. using ZedGraph;
  142.  
  143. namespace Profiling
  144. {
  145.     class ChartBuilder : IChartBuilder
  146.     {
  147.         public Control Build(List<ExperimentResult> result)
  148.         {
  149.             var chart = new ZedGraphControl(){Dock = DockStyle.Fill};
  150.  
  151.  
  152.             var sizes = new double[result.Count];
  153.             var classResults = new double[sizes.Length];
  154.             var structResults = new double[sizes.Length];
  155.             var scale = 10000;
  156.             chart.GraphPane.YAxis.Title.Text = "(Elapsed milliseconds) * "+scale;
  157.             chart.GraphPane.XAxis.Title.Text = "Amount of fields";
  158.  
  159.             for (var i = 0; i < result.Count; i++)
  160.             {
  161.                 sizes[i] = Constants.FieldCounts.ToArray()[i];
  162.                 classResults[i] = result[i].ClassResult * scale;
  163.                 structResults[i] = result[i].StructResult * scale;
  164.             }
  165.             chart.GraphPane.AddCurve("Class", sizes, classResults, Color.Red);
  166.             chart.GraphPane.AddCurve("Struct", sizes, structResults, Color.Blue);
  167.             return chart;
  168.         }
  169.     }
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. using System;
  179. using System.Text;
  180. namespace Profiling
  181. {
  182.     class Generator
  183.     {
  184.        
  185.         public static string GenerateDeclarations(){return "";}
  186.         public static string GenerateArrayRunner(){return "";}
  187.         static readonly string newline = Environment.NewLine;
  188.         static readonly string braceOpen = "{";
  189.         static readonly string braceClose = "}";
  190.         static readonly string methodBlank = "void P{0}({0} o){1}{2}";
  191.         static readonly string blockBlank =
  192. @"
  193. if ({0}isClass && size == {1})
  194. {2}
  195.    var o = new {3}(); for (int i = 0; i < count; i++) P{3}(o);
  196.    return;
  197. {4}
  198. ";
  199.         static char[] chars = new[] { 'C', 'S' };
  200.         static void AppendMethods(StringBuilder builder)
  201.         {
  202.             foreach (var i in Constants.FieldCounts)
  203.                 foreach (var ch in chars)
  204.                     builder.AppendFormat(methodBlank, "" + ch + i, braceOpen, braceClose);
  205.         }
  206.         static void AppendCalls(StringBuilder builder)
  207.         {
  208.             var inversion = "";
  209.             foreach (var i in Constants.FieldCounts)
  210.                 foreach (var ch in chars)
  211.                 {
  212.                     inversion = ch == 'C' ? "" : "!";
  213.                     builder.AppendFormat(blockBlank, inversion, i, braceOpen, "" + ch + i, braceClose);
  214.                 }
  215.         }
  216.         public static string GenerateCallRunner()
  217.         {
  218.             var header = newline + newline + "public class CallRunner  : IRunner{";
  219.             var builder = new StringBuilder(header);
  220.             AppendMethods(builder);
  221.             builder.Append("public void Call(bool isClass, int size, int count){");
  222.             AppendCalls(builder);
  223.             builder.Append("throw new ArgumentException(); }}");
  224.             return builder.ToString();
  225.         }
  226.  
  227.  
  228.  
  229.     }
  230. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top