zoukankan      html  css  js  c++  java
  • Gson 和 FastJson 性能测试

    使用版本:

        compile 'com.google.code.gson:gson:2.7'
    
        compile 'com.alibaba:fastjson:1.2.17'

    评测样板为一个People数组,People对象 中包含一个Food对象引用。各个字符串采用随机数模拟;尽量模拟列表请求数据。

    
    
    String mString  = "abcdefghijklmnopqrstuvwxyz0123456789";
    Random mRandom = new Random();

    public
    List<People> createPeopleList(int n){ List<People> list = new ArrayList<>(); for (int i=0; i< n ; i++){ list.add(createPeople()); } return list; } public People createPeople(){ People people = new People(); people.name = getRandomString(); people.age = Math.abs(mRandom.nextInt()) % 100; people.food = createFood(); return people; } public Food createFood(){ Food food = new Food(); food.name = getRandomString(); food.num = Math.abs(mRandom.nextInt()) % 10; food.price = Math.abs(mRandom.nextInt()) % 100; return food; } public String getRandomString(){ int size = Math.abs(mRandom.nextInt()) % 6; String str = ""; int len = mString.length(); for(int i=0; i< size; i++){ str += mString.charAt(Math.abs(mRandom.nextInt()) % len); } return str; }

    评测Demo:

    public String  testToJson(int n){
            mText   = "";
            List list   = createPeopleList(n);
            long startTime      = System.currentTimeMillis();
            Gson gson   = new  Gson();
            long initTime       = System.currentTimeMillis();
            String gsonStr  = gson.toJson(list);
            long parseTime      = System.currentTimeMillis();
            String gsonTime     = "gson to initTime: "+(initTime - startTime) +" parse: "+(parseTime - initTime);
    
            startTime           = System.currentTimeMillis();
            String fastStr  = JSON.toJSON(list).toString();
            parseTime           = System.currentTimeMillis();
            String fastTime     = "fast to parse: "+(parseTime - startTime);
    
            mText   = gsonTime + "
    " + fastTime;
    //        Log.d("tag", gsonStr);
    //        Log.d("tag", fastStr);
            return gsonStr;
        }
    
        public Object testFromJson(String str){
            List<People> list   = new ArrayList<>();
            long startTime      = System.currentTimeMillis();
            Gson gson   = new  Gson();
            long initTime       = System.currentTimeMillis();
            list                = gson.fromJson(str, list.getClass());
            long parseTime      = System.currentTimeMillis();
            String gsonTime     = "gson from initTime: "+(initTime - startTime) +" parse: "+(parseTime - initTime);
    
            startTime           = System.currentTimeMillis();
            list                = JSON.parseObject(str, list.getClass());
            parseTime           = System.currentTimeMillis();
            String fastTime     = "fast from parse: "+(parseTime - startTime);
    
            mText   += "
    "+gsonTime + "
    " + fastTime;
            return list;
        }

    评测机型:360 型号1503-M02, 处理器:helio X20 十核处理器, 内存4G, 系统6.0, 内核版本3.18.22+

    输出数据:

    Size 大小      GSON toJson    FastJson toJson      GSON fromJson      FastJson parseJson    单位(ms)
    20          25          39              6              3
               30          39              6              2
                27          40              6              3
                   
    200          22          15              12              11
                23          16              13              12
                22          15              11              12

    2000          116          87              43              61
                128          83              72              89             
                120          85              44               73

    20000          610          766              596              666
                  709          793              525              759
                 530          910                543              773   

    200000          6875          15394            11551              18811
                  6803          15419            10050              18718
                  6756          15217            11338              19507            


    数据分析:
    1、Size 为 20 的时候 数据偏大是因为有静态变量等相关的初始化工作,接下来的 200、2000等因为已经初始化了,所以没有相应增加时间。
    2、生成json字符串的速度,2000个对象以内,fastJson 有优势, 20000个数据以上Gson优势比较大
    3、解析json字符串的数据, 除了20个样板的极端例子,Gson 的解析性能都有可观的优势。

    总结:
    1、android开放中,按照以往经验解析json样板 不超过2000, 封装json的样板不超过200,选择Gson有一定优势。
    2、FastJson整体表现不如Gson,但是在特定样板大小中,有更好的性能。
    3、GitHub上面FastJson更新比Gson更慢
    4、建议使用Gson
    5、上述样板存在局限,没有覆盖到很多样例,具体项目中,可以实测之后在选择方案。
  • 相关阅读:
    7maven依赖排除
    5maven生命周期
    4maven依赖范围、依赖有效性
    3maven常用命令和配置依赖
    1maven概念作用
    6在Eclipse中创建maven和修改默认配置
    2maven下载与配置
    Class 'org.apache.tomcat.jdbc.pool.DataSource' not found
    17 alternatives to your default image viewer on Fedora
    zip压缩包解压后的文件名是乱码?
  • 原文地址:https://www.cnblogs.com/lipeil/p/5992488.html
Copyright © 2011-2022 走看看