java - 将三元运算符作为方法参数传递是否会影响性能
问题描述
下面只是一些示例代码,我相信有很多方法可以重构它,但问题是: 如果代码在下面的循环中以这种方式解释,三元运算符作为方法参数是否会影响代码性能。
尝试在线搜索,但没有收到任何有关性能的信息。
Class employee {
private int id;
private string firstName;
private string middleName;
private string lastName;
private string cellPhone;
private string workPhone;
private string stNumber;
private string stName;
private string city;
private string state;
private string zip;
// follow all the getter setter.
}
Class verifyEmployee {
// below loop will throw nullpointer if we don't check null conditions. Provided couple of soultions.New Code 1 and New Code 2.
ForEach(ListOfOldEmployee oldEmp:ListofOnlyEmployee){
forEach(ListOfNewEmployee newEmp: ListOfNewEmployee){
if(firstname based on some condition call verify){
verify(someobject, oldEmp.getfirstName(),newEmp.getfirstName())
} else if(middleName based on some condition){
verify(someobject, oldEmp.getmiddleName(),newEmp.getmiddleName())
} else if(lastName based on some condition){
verify(someobject, oldEmp.getlastName(),newEmp.getlastName())
} else if(cellPhone based on some condition){
verify(someobject, oldEmp.getcellPhone(),newEmp.getcellPhone())
} else if(workPhone based on some condition call verify){
verify(someobject, oldEmp.getworkPhone(),newEmp.getworkPhone())
} else if(stNumber based on some condition){
verify(someobject, oldEmp.getstNumber(),newEmp.getstNumber())
} else if(stName based on some condition){
verify(someobject, oldEmp.getstName(),newEmp.getstName())
} else if(city based on some condition){
verify(someobject, oldEmp.getcity(),newEmp.getcity())
} else if(state based on some condition){
verify(someobject, oldEmp.getstate(),newEmp.getstate())
} else if(zip based on some condition){
verify(someobject, oldEmp.getzip(),newEmp.getzip())
}
/// --- etc follows
}
}
verify(someobject,string,string){ ---- method I don't send the whole object.
// process something here here.
}
}
新代码:1 在调用 forloop 之前检查非空条件
Class verifyEmployee {
--------- without ternary oprator use code looks so weird and lengthy.
string firstNameOldEmp = null;
string middleNameOldEmp = null;
string lastNameOldEmp = null;
string cellPhoneOldEmp = null;
string workPhoneOldEmp = null;
string stNumberOldEmp = null;
string stNameOldEmp = null;
string cityOldEmp = null;
string stateOldEmp = null;
string zipOldEmp = null;
string firstNameNewEmp = null;
string middleNameNewEmp = null;
string lastNameNewEmp = null;
string cellPhoneNewEmp = null;
string workPhoneNewEmp = null;
string stNumberNewEmp = null;
string stNameNewEmp = null;
string cityNewEmp = null;
string stateNewEmp = null;
string zipNewEmp = null;
if(OldEmp!=null){
string firstNameOldEmp = oldEmp.getfirstName();
string middleNameOldEmp = oldEmp.getmiddleName();
string lastNameOldEmp = oldEmp.getlastName();
string cellPhoneOldEmp = oldEmp.getcellPhone();
string workPhoneOldEmp = oldEmp.getworkPhone();
string stNumberOldEmp = oldEmp.getstNumber();
string stNameOldEmp = oldEmp.getName();
string cityOldEmp = oldEmp.getcity();
string stateOldEmp = oldEmp.getstate();
string zipOldEmp = oldEmp.getzip();
}
if(newEmp!=null){
string firstNameNewEmp = newEmp.getfirstName();
string middleNameNewEmp = newEmp.getmiddleName();
string lastNameNewEmp = newEmp.getlastName();
string cellPhoneNewEmp = newEmp.getcellPhone();
string workPhoneNewEmp = newEmp.getworkPhone();
string stNumberNewEmp = newEmp.getstNumber();
string stNameNewEmp = newEmp.getName();
string cityNewEmp = newEmp.getcity();
string stateNewEmp = newEmp.getstate();
string zipNewEmp = newEmp.getzip();
}
ForEach(ListOfOldEmployee oldEmp:ListofOnlyEmployee){
forEach(ListOfNewEmployee newEmp: ListOfNewEmployee){
if(firstname based on some condition call verify){
verify(someobject, firstNameOldEmp,firstNameNewEmp)
} else if(middleName based on some condition){
verify(someobject, middleNameOldEmp,middleNameNewEmp)
} else if(lastName based on some condition){
verify(someobject, lastNameOldEmp,lastNameNewEmp)
} else if(cellPhone based on some condition){
verify(someobject, cellPhoneOldEmp,cellPhoneNewEmp)
} else if(workPhone based on some condition call verify){
verify(someobject, workPhoneOldEmp,workPhoneNewEmp)
} else if(stNumber based on some condition){
verify(someobject, stNumberOldEmp,stNumberNewEmp)
} else if(stName based on some condition){
verify(someobject, stNameOldEmp,stNameNewEmp)
} else if(city based on some condition){
verify(someobject, cityOldEmp,cityNewEmp)
} else if(state based on some condition){
verify(someobject, stateOldEmp),stateNewEmp)
} else if(zip based on some condition){
verify(someobject, zipOldEmp,zipNewEmp)
}
/// --- etc follows
}
}
新代码:2 -- 改用三元运算符
Class verifyEmployee {
--- remove all initlization and use the ternary operator instead. Questions is does this hampper the performance if it's used as method argument
--- and we will be checking not null conditions everytime in the loop of list
ForEach(ListOfOldEmployee oldEmp:ListofOnlyEmployee){
forEach(ListOfNewEmployee newEmp: ListOfNewEmployee){
if(firstname based on some condition call verify){
verify(someobject, oldEmp!=null?oldEmp.getfirstName():null,newEmp!=null?newEmp.getfirstName():null)
} else if(middleName based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getmiddleName():null,newEmp!=null?newEmp.getmiddleName():null)
} else if(lastName based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getlastName():null,newEmp!=null?newEmp.getlastName():null)
} else if(cellPhone based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getcellPhone():null,newEmp!=null?newEmp.getcellPhone():null)
} else if(workPhone based on some condition call verify){
verify(someobject, oldEmp!=null?oldEmp.getworkPhone():null,newEmp!=null?newEmp.getworkPhone():null)
} else if(stNumber based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getstNumber():null,newEmp!=null?newEmp.getstNumber():null)
} else if(stName based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getstName():null,newEmp!=null?newEmp.getstName():null)
} else if(city based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getcity():null,newEmp!=null?newEmp.getcity():null)
} else if(state based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getstate():null,newEmp!=null?newEmp.getstate():null)
} else if(zip based on some condition){
verify(someobject, oldEmp!=null?oldEmp.getzip():null,newEmp!=null?newEmp.getzip():null)
}
/// --- etc follows
}
}
}
解决方案
要回答您关于性能的问题,我怀疑是否存在任何实质性差异。您可以在代码前后使用 System.nanoTime() 检查性能,并查看每种方法需要多长时间。但是,请注意 JIT 优化;您应该循环运行代码并查看时间示例。我发现第一次通过很慢,其他人相当稳定。
正如评论所指出的,第二种解决方案很难阅读。出于这个原因,我建议使用第一个。
推荐阅读
- r - 根据数据框的名称列出数据框(按名称调用数据框)
- netlogo - 如何将具有随机值的属性分配给海龟?
- java - QueryDsl - 创建一个谓词来检查 localdate 是否是当前周的一部分
- c - 在linux中使用strchr查找绝对路径
- shopify-app - 根据商品编号打印不同商品的折扣值
- swift - Swift SceneKit 也不是圆形球体
- java - 创建事件后线程“AWT-EventQueue-0”java.lang.NoClassDefFoundError 中的异常
- bash - 如何使用终端从csv文件中提取列
- c - getaddrinfo 过期值(Linux 主机名条目)
- database - Cassandra 如何在其 SSTables 中高效地对可变长度行进行二进制搜索?