Skip to content

Instantly share code, notes, and snippets.

@mehmetcantas
Created April 23, 2020 22:55
Show Gist options
  • Save mehmetcantas/ffe642cf52cd84e735871b0fd658e455 to your computer and use it in GitHub Desktop.
Save mehmetcantas/ffe642cf52cd84e735871b0fd658e455 to your computer and use it in GitHub Desktop.
class Program
{
const int limit = 300000;
static void Main(string[] args)
{
var plusOperatorResult = WithPlusOperator();
var stringBuildeResult = WithStringBuilder();
Console.WriteLine("Plus Operator Result : " + plusOperatorResult);
Console.WriteLine("String Builder Result : " + stringBuildeResult);
Console.ReadLine();
}
static long WithPlusOperator()
{
string variable = "";
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < limit; i++)
{
variable += "x";
}
watch.Stop();
return watch.ElapsedMilliseconds;
}
static long WithStringBuilder()
{
StringBuilder builder = new StringBuilder();
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < limit; i++)
{
builder.Append("x");
}
watch.Stop();
return watch.ElapsedMilliseconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment