Last active
September 1, 2016 11:44
-
-
Save bbowyersmyth/2cc93ec0b2127cd98d567654b5d6467e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.Text; | |
namespace ConsoleApp2 | |
{ | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
Profile("Ctor() perf", 10000000, () => | |
{ | |
var a = new StringBuilder(); | |
}); | |
Profile("Ctor(int) perf", 10000000, () => | |
{ | |
var a = new StringBuilder(16); | |
}); | |
var sb = new StringBuilder(); | |
sb.Append("abcdef"); | |
Profile("ToString perf", 10000000, () => | |
{ | |
sb.ToString(); | |
}); | |
} | |
static void Profile(string description, int iterations, Action func) | |
{ | |
// warm up | |
func(); | |
// clean up | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
GC.Collect(); | |
var watch = new Stopwatch(); | |
watch.Start(); | |
for (int i = 0; i < iterations; i++) | |
{ | |
func(); | |
} | |
watch.Stop(); | |
Console.Write(description); | |
Console.WriteLine(" Time Elapsed {0} ms", watch.ElapsedMilliseconds); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment