Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ArcticEcho
Last active December 25, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArcticEcho/6963107 to your computer and use it in GitHub Desktop.
Save ArcticEcho/6963107 to your computer and use it in GitHub Desktop.
namespace C_Sharp_Benchmarker
{
class Program
{
private static int testIterations = 10000000;
public static void Main(string[] args)
{
var t1 = Test1();
var t2 = Test2();
if (t1 < t2) Console.WriteLine("Test 1 is " + ((t2 / t1) * 100).ToString() + "% faster than test 2.");
if (t2 < t1) Console.WriteLine("Test 2 is " + ((t1 / t2) * 100).ToString() + "% faster than test 1.");
}
public static decimal Test1()
{
Console.Write("Running test 1.....");
Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < testIterations; i++)
{
TestMethod1();
}
s.Stop();
Console.WriteLine("Duration: " + s.ElapsedMilliseconds.ToString() + " milliseconds");
return s.ElapsedMilliseconds;
}
public static decimal Test2()
{
Console.Write("Running test 2.....");
Stopwatch s = new Stopwatch();
s.Start();
for (int i = 0; i < testIterations; i++)
{
TestMethod2();
}
s.Stop();
Console.WriteLine("Duration: " + s.ElapsedMilliseconds.ToString() + " milliseconds");
return s.ElapsedMilliseconds;
}
public static void TestMethod1()
{
var myList = new List<string> { "12", "13", "14" };
string myString = String.Join(",", myList);
}
public static void TestMethod2()
{
var myList = new List<string> { "12", "13", "14" };
var myString = "";
var sb = new System.Text.StringBuilder();
foreach (string s in myList)
{
sb.Append(s);
sb.Append(",");
}
myString = sb.ToString();
myString = myString.Substring(0, myString.Length - 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment