Skip to content

Instantly share code, notes, and snippets.

@danielmoore
Created September 24, 2011 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmoore/1238996 to your computer and use it in GitHub Desktop.
Save danielmoore/1238996 to your computer and use it in GitHub Desktop.
Testing perf of different representations of combined conditional logic
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