Skip to content

Instantly share code, notes, and snippets.

@bbowyersmyth
Last active September 1, 2016 11:44
Show Gist options
  • Save bbowyersmyth/2cc93ec0b2127cd98d567654b5d6467e to your computer and use it in GitHub Desktop.
Save bbowyersmyth/2cc93ec0b2127cd98d567654b5d6467e to your computer and use it in GitHub Desktop.
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