工具Android开发经验谈Android知识

Android比较场景(Java)性能测试.md

2017-08-14  本文已影响39人  码农豹

场景:社交类app会有好友模块需求,假设好友最多有一千个,好友列表的显示会有多次回调填充字段的操作,如先拉去整个好友列表,再拉去好友是否在线的状态字段,那么回调的数据需要回填到原先的数据中,回填填数据时可能出现两个for循环比较id后填充字段的场景,时间复杂度为n^2,n = 1000时是百万次的比较及赋值,所以才萌生了测试性能的想法,指导是否需要更改时间复杂度,子线程操作,语句优化。

比较一:电脑与手机性能的比较

public class main {
    static SubNoninstanctiableClass class1;
    static ArrayList<String> list = new ArrayList<String>();
    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
        new SubNoninstanctiableClass();
        System.out.println("million" + System.currentTimeMillis());
        for (int i = 0; i < 1000; i++) {
            for (int j = 0; j < 1000; j++) {
                list.add(new String("hihi" +  i +  j));
            }
        }
        System.out.println("million" + System.currentTimeMillis());
    
    }
}

比较二:add new String 与add new 一般性bean

public class TestBean {
    long uid;
    int age;
    String name;
    int height;
    String name1;

    public TestBean(long i) {
        uid = i;
    }

    public TestBean(long i, String s) {
        uid  = i;
        name = s;
    }
}

比较三:已有两个size为1000的list,取uid比较并赋值,如果比较条件增加或赋值语句增加对耗时的影响

  for (int i = 0; i < 1000; i++) {
                    list1.add(new TestBean(100+ i));
                }
                for (int i = 0; i < 1000; i++) {
                    list2.add(new TestBean(100+ i,"tom" + i));
                }
                start = System.currentTimeMillis();
                System.out.println("million" + start);
                for (int i = 0; i < 1000; i++) {
                    for (int j = 0; j < 1000; j++) {
                        if (list1.get(i).uid == list2.get(j).uid ){
                            list1.get(i).name = list2.get(j).name;
                        }
                    }
                }
                end = System.currentTimeMillis();
                System.out.println("million" +  (end - start));
}
//将11-13行的代码替换为如下:增加比较条件及赋值语句
            if (list1.get(i).uid == list2.get(j).uid && list1.get(i).uid == list2.get(j).uid &&list1.get(i).uid == list2.get(j).uid ){
                            list1.get(i).name = list2.get(j).name;
                            list1.get(i).name = "hahah";
                            list1.get(i).name = "hahahaa";
             }

比较四:已有两个size为1000的list,取uid比较并赋值(比较三),如果增加if个数对耗时的影响

for (int i = 0; i < 1000; i++) {
           for (int j = 0; j < 1000; j++) {
                        if (list1.get(i).uid == list2.get(j).uid && list1.get(i).uid == list2.get(j).uid &&list1.get(i).uid == list2.get(j).uid ){
                            list1.get(i).name = list2.get(j).name;
                            list1.get(i).name = "hahah";
                            list1.get(i).name = "hahahaa";
                        }
                        //增加的if
                        if (list1.get(i).uid == list2.get(j).uid ){
                            list1.get(i).name = "hahah";
                        }
                        //增加的if
                        if (list1.get(i).uid == list2.get(j).uid ){
                            list1.get(i).name = "hahahaa";
                        }
          }
}

好友场景结论:当好友达到上千个,如果Bean为类似TestBean,按N^2的时间复杂度耗时为2-4S,所以应该改变存储结构,如用hashmap代替list,将复杂度降为nlogn,同时注意切换子线程进行操作防止ANR。

上一篇下一篇

猜你喜欢

热点阅读