Gson 和 FastJson 性能测试

使用版本:android

    compile 'com.google.code.gson:gson:2.7'

    compile 'com.alibaba:fastjson:1.2.17'

 

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

 

 
 
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 + "\n" + 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   += "\n"+gsonTime + "\n" + fastTime;
        return list;
    }

 

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

输出数据:性能

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              122000          116          87              43              61            128          83              72              89                         120          85              44               7320000          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            数据分析:一、Size 为 20 的时候 数据偏大是由于有静态变量等相关的初始化工做,接下来的 200、2000等由于已经初始化了,因此没有相应增长时间。二、生成json字符串的速度,2000个对象之内,fastJson 有优点, 20000个数据以上Gson优点比较大三、解析json字符串的数据, 除了20个样板的极端例子,Gson 的解析性能都有可观的优点。总结:一、android开放中,按照以往经验解析json样板 不超过2000, 封装json的样板不超过200,选择Gson有必定优点。二、FastJson总体表现不如Gson,可是在特定样板大小中,有更好的性能。三、GitHub上面FastJson更新比Gson更慢四、建议使用Gson五、上述样板存在局限,没有覆盖到不少样例,具体项目中,能够实测以后在选择方案。
相关文章
相关标签/搜索