Skip to content

Instantly share code, notes, and snippets.

@vkbandi
Created September 16, 2015 19:52
Show Gist options
  • Save vkbandi/3efe3fb044227a65be67 to your computer and use it in GitHub Desktop.
Save vkbandi/3efe3fb044227a65be67 to your computer and use it in GitHub Desktop.
public class ConditionalPerformance
{
//If block
public long WithOnlyIf(int myFlag)
{
Stopwatch myTimer = new Stopwatch();
string someString = "";
myTimer.Start();
for (int i = 0; i < 1000000; i++)
{
string height = "80%";
string width = "80%";
if (myFlag == 1)
{
height = "60%";
width = "60%";
}
someString = "Height: " + height + Environment.NewLine + "Width: " + width;
}
myTimer.Stop();
File.WriteAllText("testif.txt", someString);
return myTimer.ElapsedMilliseconds;
}
//If-else block
public long WithIfAndElse(int myFlag)
{
Stopwatch myTimer = new Stopwatch();
string someString = "";
myTimer.Start();
for (int i = 0; i < 1000000; i++)
{
string height;
string width;
if (myFlag == 1)
{
height = "60%";
width = "60%";
}
else
{
height = "80%";
width = "80%";
}
someString = "Height: " + height + Environment.NewLine + "Width: " + width;
}
myTimer.Stop();
File.WriteAllText("testifelse.txt", someString);
return myTimer.ElapsedMilliseconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment