0

How much do conditionals effect performance? For example, would code A execute faster than code B since it is only checking the boolean value once? (data is a DataTable in this example)

Code A:

bool isBusiness = string.IsNullOrEmpty(data["businessName") ? false : true;
if(isBusiness) {
    var name = data["businessName"];
    var id = data["businessId"];
    var phone = data["businessPhone"];
    var address = data["businessAddress"];
}
else {
    var name = data["customerName"];
    var id = data["customerId"];
    var phone = data["customerPhone"];
    var address = data["customerAddress"];
}

Code B:

bool isBusiness = string.IsNullOrEmpty(data["businessName") ? false : true;
var name = isBusiness ? data["businessName"] : data["customerName"];
var id = isBusiness ? data["businessId"] : data["customerId"];
var phone = isBusiness ? data["businessPhone"] : data["customerPhone"];
var address = isBusiness ? data["businessAddress"] : data["customerAddress"];

This is a small example so the actual difference would be small, but what if I were mapping hundreds of rows like this? Some care about the isBusiness flag and some don't. Does anyone have any statistical evidence one way or the other?

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
gwin003
  • 7,432
  • 5
  • 38
  • 59
  • it is taking one cpu cycle to check, so hundreds of rows is nothing – binboavetonik Oct 25 '13 at 14:57
  • Why asking? Measure it. Besides that i would expect the Indexer access via string the slowest thing in your code. If you want to optimize your code look at how your data class does that. – Ralf Oct 25 '13 at 14:57
  • 1
    If you have performance problems here, it's surely not caused by the `if` statements. It's probably caused by the HORRIBLE, magic-string-based, untyped DataTable stuff where everything is `object` and needs to be casted / boxed/unboxed at runtime. Create a proper data model or use an ORM instead. – Federico Berasategui Oct 25 '13 at 15:00
  • 4
    If you want to find out how fast your horse runs, do you ask strangers on the internet or do you time your horse? – Eric Lippert Oct 25 '13 at 15:07
  • @HighCore I am not having any performance issues, this was merely a thought. Unfortunately, I need to use the "HORRIBLE, magic-string-based, untyped DataTable stuff" because I am mapping over 1000 fields spread over 10 mapping classes. And due to the requirements, I need to use a DataTable. – gwin003 Oct 25 '13 at 17:37
  • @gwin003 if you don't have performance issues why are you even worrying about that? I'd rather care about removing the horrible code and doing something more maintainable. – Federico Berasategui Oct 25 '13 at 17:41
  • @HighCore I'm not worrying about it, just asking because I was curious. – gwin003 Oct 25 '13 at 18:08

2 Answers2

0

I don't think there is a clear answer for this question. It depends on how many rows etc, the only one who can answer it is you. To test it you can simply use the Stopwatch class.

I don't think there will be many/any difference, so I would say A, since it is more readable/editable.

Maximc
  • 1,722
  • 1
  • 23
  • 37
0

I agree with other responders here; however, I would like to add to this.

The performance hit incurred by if statements is something to be taken into consideration if a given method will be run often. If you would like to learn more about the specifics of this effecting performance, I recommend reading this post about conditional branching.

That being said, I will say that you get a pretty much Insignificant Performance Boost by changing the first line of code from this:

bool isBusiness = string.IsNullOrEmpty(data["businessName") ? false : true;

to this:

bool isBusiness = !string.IsNullOrEmpty(data["businessName");

Though, out of the two options I prefer the first one

The reason for this, is that if you would like to push for some code clarity, you could refactor it from this:

bool isBusiness = string.IsNullOrEmpty(data["businessName") ? false : true;
if(isBusiness) {
   var name = data["businessName"];
   var id = data["businessId"];
   var phone = data["businessPhone"];
   var address = data["businessAddress"];
}
else {
   var name = data["customerName"];
   var id = data["customerId"];
   var phone = data["customerPhone"];
   var address = data["customerAddress"]; 
}

to this:

bool isBusiness = !string.IsNullOrEmpty(data["businessName");
if(isBusiness) {
   this.MapBusinessRow(data);
}
else {
   this.MapCustomerRow(data)
}

Though, I must say, that is purely for readability and is not for performance.

At the end of the day, if you are worried about it; Test It!

Community
  • 1
  • 1
GrantByrne
  • 849
  • 10
  • 21