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?