条件对性能有多大影响?例如,代码 A 是否会比代码 B 执行得更快,因为它只检查布尔值一次?(在本例中data
为 a )DataTable
代码 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"];
}
代码 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"];
这是一个小例子,所以实际差异会很小,但是如果我像这样映射数百行呢?有些人关心isBusiness
国旗,有些人不关心。有人有任何统计证据吗?