纯字符串assemble和parser比较,fastjson 比 json要至少慢5倍

前端之家收集整理的这篇文章主要介绍了纯字符串assemble和parser比较,fastjson 比 json要至少慢5倍前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package com.xhtt.ifserver.nio;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

/**
 * auth: WenYF
 * date: 2016/11/26
 */
public class JsonPerformenceTest {
    static int max = 1000;
    static int arrayMax = 100;

    private static String str;
    static {
        JSONObject json = new JSONObject();
        try {
            for (int i = 0; i < max ; i ++) {
                json.put("key" + i,"valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
            }
            JSONArray array = new JSONArray();
            for (int i = 0; i < arrayMax; i++) {
                array.put("arrayarrayarrayarrayarray" + i);
            }

            json.put("array",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println(json.toString());
        str = json.toString();
    }

    @Test
    public void testJsonCreate() {
        JSONObject json = new JSONObject();
        try {
            for (int i = 0; i < max ; i ++) {
                json.put("key" + i,"valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
            }

            JSONArray array = new JSONArray();
            for (int i = 0; i < arrayMax; i++) {
                array.put("arrayarrayarrayarrayarray" + i);
            }

            json.put("array",array);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println(json.toString());
    }

    @Test
    public void testFastJsonCreate() {
        com.alibaba.fastjson.JSONObject json = new com.alibaba.fastjson.JSONObject();

        for (int i = 0; i < max ; i ++) {
            json.put("key" + i,"valuevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevaluevalue" + i);
        }

        com.alibaba.fastjson.JSONArray array = new com.alibaba.fastjson.JSONArray();
        for (int i = 0; i < arrayMax; i++) {
            array.add("arrayarrayarrayarrayarray" + i);
        }
        json.put("array",array);

        System.out.println(json.toString());
    }

    @Test
    public void testJsonParser() {
        try {
            JSONObject json = new JSONObject(str);
            for (int i = 0; i < max ; i ++) {
                json.get("key" + i);
            }
            int i = json.getJSONArray("array").length();
            System.out.println("size = " + i);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testFastJsonParser() {
        com.alibaba.fastjson.JSONObject json = JSON.parSEObject(str);
        for (int i = 0; i < max ; i ++) {
            json.get("key" + i);
        }

        int i = json.getJSONArray("array").size();
        System.out.println("size = " + i);
    }
}

json.jar | 50c3afb2166798c5de0896b90659d8a2b2f8fcec | http://www.json.org/license.html

fastjson |1.2.21 |https://github.com/alibaba/fastjson

原文链接:https://www.f2er.com/json/289109.html

猜你在找的Json相关文章