Created
September 16, 2015 19:52
-
-
Save vkbandi/3efe3fb044227a65be67 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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