Advertisement
Fusty03

Benchmark

Dec 15th, 2019
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Net.WebSockets;
  5. using System.Security.AccessControl;
  6. using System.Text;
  7. using NUnit.Framework;
  8.  
  9. namespace StructBenchmarking
  10. {
  11.     public class Benchmark : IBenchmark
  12.     {
  13.         public double MeasureDurationInMs(ITask task, int repetitionCount)
  14.         {
  15.             GC.Collect();                  
  16.             GC.WaitForPendingFinalizers();  
  17.                                            
  18.             var watch = new Stopwatch();
  19.             task.Run();
  20.             watch.Start();
  21.             for (var i = 0; i < repetitionCount; i++)
  22.             {
  23.                 task.Run();
  24.             }
  25.             watch.Stop();
  26.             return (double)watch.ElapsedMilliseconds / repetitionCount;
  27.         }
  28.     }
  29.  
  30.     public class ForStringBuilder : ITask
  31.     {
  32.         private int repetitionCount;
  33.         private char letter;
  34.  
  35.         public ForStringBuilder(int repetitionCount, char letter)
  36.         {
  37.             this.repetitionCount = repetitionCount;
  38.             this.letter = letter;
  39.         }
  40.        
  41.         string WithStringBuilder()
  42.         {
  43.             var strBuilder = new StringBuilder();
  44.             for (var i = 0; i < repetitionCount; i++)
  45.                 strBuilder.Append(letter);
  46.             return strBuilder.ToString();
  47.         }
  48.        
  49.         public void Run()
  50.         {
  51.             WithStringBuilder();
  52.         }
  53.     }
  54.  
  55.     public class ForConstructor : ITask
  56.     {
  57.         private int repetitionCount;
  58.         private char letter;
  59.  
  60.         public ForConstructor(int repetitionCount, char letter)
  61.         {
  62.             this.repetitionCount = repetitionCount;
  63.             this.letter = letter;
  64.         }
  65.        
  66.         string WithConstructor()
  67.         {
  68.             return new string(letter, repetitionCount);
  69.         }
  70.  
  71.          public void Run()
  72.          {
  73.              WithConstructor();
  74.          }
  75.     }
  76.  
  77.     [TestFixture]
  78.     public class RealBenchmarkUsageSample
  79.     {
  80.         [Test]
  81.         public void StringConstructorFasterThanStringBuilder()
  82.         {
  83.             var benchmark = new Benchmark();
  84.             var forConstructor = new ForConstructor(4000000, 'a');
  85.             var forBuilder = new ForStringBuilder(4000000, 'a');
  86.            
  87.             var builderTime = benchmark.MeasureDurationInMs(forBuilder, 20);
  88.             var constructorTime = benchmark.MeasureDurationInMs(forConstructor, 20);
  89.             Assert.Less(constructorTime, builderTime);
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement