Created
September 24, 2011 05:12
-
-
Save danielmoore/1238996 to your computer and use it in GitHub Desktop.
Testing perf of different representations of combined conditional logic
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
void Main() | |
{ | |
Test(PerformIfBlocks).Dump("If blocks"); | |
Test(PerformLogicalAnd).Dump("Logical AND"); | |
Test(PerformLogicalOr).Dump("Logical OR"); | |
} | |
private int Test(Func<int, int, bool> test) { | |
var cancel = false; | |
var task = Task.Factory.StartNew(() => { | |
int completed = 0; | |
int arg2 = 0; | |
while (!cancel) | |
test(completed++, arg2 += 4); | |
return completed; | |
}); | |
Thread.Sleep(3000); | |
cancel = true; | |
return task.Result; | |
} | |
bool PerformLogicalAnd(int arg1, int arg2){ | |
return arg1 < arg2 && arg2 - arg1 < 100 && arg1 + arg2 > 42 && arg2 % arg1 != 0; | |
} | |
bool PerformLogicalOr(int arg1, int arg2){ | |
return !(arg1 < arg2) || !(arg2 - arg1 < 100) || !(arg1 + arg2 > 42) || !(arg2 % arg1 != 0); | |
} | |
bool PerformIfBlocks(int arg1, int arg2){ | |
if(!(arg1 < arg2)) return false; | |
if(!(arg2 - arg1 < 100)) return false; | |
if(!(arg1 + arg2 > 42)) return false; | |
if(!(arg2 % arg1 != 0)) return false; | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment