将三元运算符作为方法参数传递是否会妨碍性能

时间:2018-07-20 15:37:49

标签: java ternary-operator

下面只是一些示例代码,我敢肯定有很多方法可以对其进行重构,但问题是: 如果代码在下面的循环中进行了解释,则三元运算符作为方法参数是否会妨碍代码性能。

试图在线搜索,但未收到任何有关效果的信息。

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之前检查not null条件

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
        }
    }
}

1 个答案:

答案 0 :(得分:-1)

要回答您有关性能的问题,我怀疑是否存在实质性差异。您可以在代码前后使用System.nanoTime()检查性能,并查看每种方法花费的时间。但是,要提防JIT优化。您应该循环运行代码并查看时间示例。我发现第一遍很慢,其他遍历很稳定。

正如评论所指出的,第二种解决方案很难阅读。因此,我建议使用第一个。