加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 18:51:19 所属栏目:百科 来源:网络整理
导读: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
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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读