考虑清洁代码和应用程​​序性能,哪种实现更好?

时间:2018-05-10 12:49:25

标签: java

第一个选项不会每次都创建一个新对象,但可读性较差。其次,创建一个对象,但看起来很干净。 PoolList大小约为20-28。你会选择哪一个?

for (int i = 0; i < poolList.getPoolList().size(); i++){

        if (isTaskExecutable(poolList.getPoolList().get(i).getName()))
            callableList.add(new ConnectToApiCallable(this.appendPoolApiUrl(poolList.getPoolList().get(i)), poolList.getPoolList().get(i).getName(), poolList.getPoolList().get(i).getType()));
        else
            continue;
}
for (int i = 0; i < poolList.getPoolList().size(); i++){

        PoolDefinition poolDefinition = poolList.getPoolList().get(i);

        if (isTaskExecutable(poolDefinition.getName()))
            callableList.add(new ConnectToApiCallable(this.appendPoolApiUrl(poolDefinition), poolDefinition.getName(), poolDefinition.getType()));
        else
            continue;
}

2 个答案:

答案 0 :(得分:4)

第二个选项创建一个新对象。它只是在poolList.getPoolList().get(i)创建对现有对象的堆栈引用。这样你就不必反复承担调用poolList.getPoolList().get(i)来获取同一个对象的开销。

所以第二个选项实际上比第一个选项更清晰

答案 1 :(得分:1)

事实上,我不明白为什么Second Option会更快,因此我做了这个模拟类似场景的测试:

<script>

<强>输出

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Person {
    private String name;
    private int age;
    private List<Person> friends;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
        friends = new ArrayList<>();
    }

    public void addFriend(Person person) {
        friends.add(person);
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

   public List<Person> getFreinds(){
       return friends;
   }


   public static void main(String[] args) {
        char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        Random rand = new Random();
        List<Person> people = new ArrayList<>();
        for(int i=0; i<1000000; i++) {
            String name = "";
            for(int j=0; j<10; j++) {
                name += alphabet[rand.nextInt(26)];
            }       
            people.add(new Person(name, rand.nextInt(60)+10));
        }


        Person person = people.get(0);
        for(Person p : people) person.addFriend(p);


        final long startTime1 = System.currentTimeMillis();
        for (int i = 0; i < person.getFreinds().size(); i++){
            person.getFreinds().get(i).getAge();
            person.getFreinds().get(i).getName();
        }
        final long endTime1 = System.currentTimeMillis();
        System.out.println("First Option Using Method Chaining: " + (endTime1-startTime1) + " Milliseconds");


        final long startTime2 = System.currentTimeMillis();
        for (int i = 0; i < person.getFreinds().size(); i++){
            Person friend = person.getFreinds().get(i);
            friend.getAge();
            friend.getName();
        }
        final long endTime2 = System.currentTimeMillis();
        System.out.println("Second Option Using Local Object: " + (endTime2-startTime2) + " Milliseconds");
    }
}

<强>结论

对一百万个元素进行测试并重复多次给出相同的结果: 实际上,两个方法调用将花费稍微更多的时间。