Advertisement
Guest User

Console vs. Debug

a guest
Nov 5th, 2017
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6.  
  7. namespace Console_vs_Debug
  8. {
  9.     class Program
  10.     {
  11.         class Trial
  12.         {
  13.             public string name;
  14.             public Action console;
  15.             public Action debug;
  16.             public List<float> consoleMeasuredTimes = new List<float>();
  17.             public List<float> debugMeasuredTimes = new List<float>();
  18.         }
  19.  
  20.         static Stopwatch sw = new Stopwatch();
  21.         private static int repeatLoop = 1000;
  22.         private static int iterations = 2;
  23.         private static int dummy = 0;
  24.  
  25.         static void Main(string[] args) {
  26.             if (args.Length == 2) {
  27.                 repeatLoop = int.Parse(args[0]);
  28.                 iterations = int.Parse(args[1]);
  29.             }
  30.  
  31.             // do some dummy work
  32.             for (int i = 0; i < 100; i++) {
  33.                 Console.WriteLine("-");
  34.                 Debug.WriteLine("-");
  35.             }
  36.  
  37.             for (int i = 0; i < iterations; i++) {
  38.                 foreach (Trial trial in trials) {
  39.                     Thread.Sleep(50);
  40.                     sw.Restart();
  41.                     for (int r = 0; r < repeatLoop; r++)
  42.                         trial.console();
  43.                     sw.Stop();
  44.                     trial.consoleMeasuredTimes.Add(sw.ElapsedMilliseconds);
  45.                     Thread.Sleep(1);
  46.                     sw.Restart();
  47.                     for (int r = 0; r < repeatLoop; r++)
  48.                         trial.debug();
  49.                     sw.Stop();
  50.                     trial.debugMeasuredTimes.Add(sw.ElapsedMilliseconds);
  51.  
  52.                 }
  53.             }
  54.             Console.WriteLine("---\r\n");
  55.             foreach (Trial trial in trials) {
  56.                 var consoleAverage = trial.consoleMeasuredTimes.Average();
  57.                 var debugAverage = trial.debugMeasuredTimes.Average();
  58.                 Console.WriteLine(trial.name);
  59.                 Console.WriteLine($"    console: {consoleAverage,11:F4}");
  60.                 Console.WriteLine($"      debug: {debugAverage,11:F4}");
  61.                 Console.WriteLine($"{consoleAverage / debugAverage,32:F2} (console/debug)");
  62.                 Console.WriteLine();
  63.             }
  64.  
  65.             Console.WriteLine("all measurements are in milliseconds");
  66.             Console.WriteLine("anykey");
  67.             Console.ReadKey();
  68.         }
  69.  
  70.         private static List<Trial> trials = new List<Trial> {
  71.             new Trial {
  72.                 name = "constant",
  73.                 console = delegate {
  74.                         Console.WriteLine("A static and constant string");
  75.                 },
  76.                 debug = delegate {
  77.                     Debug.WriteLine("A static and constant string");
  78.                 }
  79.             },
  80.             new Trial {
  81.                 name = "dynamic",
  82.                 console = delegate {
  83.                     Console.WriteLine("A dynamically build string (number " + dummy++ + ")");
  84.                 },
  85.                 debug = delegate {
  86.                         Debug.WriteLine("A dynamically build string (number " + dummy++ + ")");
  87.                 }
  88.             },
  89.             new Trial {
  90.                 name = "interpolated",
  91.                 console = delegate {
  92.                     Console.WriteLine($"An interpolated string (number {dummy++,6})");
  93.                 },
  94.                 debug = delegate {
  95.                     Debug.WriteLine($"An interpolated string (number {dummy++,6})");
  96.                 }
  97.             }
  98.         };
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement