Skip to content

Instantly share code, notes, and snippets.

@luisdeol
Created May 26, 2020 11:09
Show Gist options
  • Save luisdeol/29abafbd2a36070f526019034ed6365e to your computer and use it in GitHub Desktop.
Save luisdeol/29abafbd2a36070f526019034ed6365e to your computer and use it in GitHub Desktop.
3.5: Using the Stopwatch class
using System;
using System.Diagnostics;
using System.Text;
namespace _35_ApplicationDiagnostics
{
class Program
{
static void Main(string[] args)
{
var stopWatch = new Stopwatch();
Console.WriteLine("Prepare...");
stopWatch.Start();
Console.WriteLine($"Stopwatch just started! Elapsed Time: {stopWatch.Elapsed:mm\\:ss\\.ff}");
var string1 = ConcatenateStringUsingStringBuilder();
stopWatch.Stop();
Console.WriteLine($"First stop: Elapsed time for ConcatenateString is {stopWatch.ElapsedMilliseconds}");
stopWatch.Reset();
stopWatch.Start();
var string2 = ConcatenateString();
stopWatch.Stop();
Console.WriteLine($"First stop: Elapsed time for ConcatenateString is {stopWatch.ElapsedMilliseconds}");
Console.Read();
}
public static string ConcatenateString()
{
var word = string.Empty;
for (var i = 0; i < 100000; i++)
{
word += "a";
}
return word;
}
public static string ConcatenateStringUsingStringBuilder()
{
var word = new StringBuilder();
for (var i = 0; i < 100000; i++)
{
word.Append("a");
}
return word.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment