每一年都奔走在自己热爱里

没有人是一座孤岛,总有谁爱着你

fastJson和jackson性能对比

   public static void main(String [] str) {
        Map map = new HashMap<String,Object>();
        map = getMapData(map,1000000);
        JSONObject fastJson = new JSONObject();
        long fastJsonBegin =  System.currentTimeMillis();

        //fastJson
        System.out.println("fastJson-------------------------------------------------");
        // System.out.println("fastJson"+fastJson.toJSONString(map));
        fastJson.toJSONString(map);
        long fastJsonEnd =  System.currentTimeMillis();
        System.out.println("fastJson-------------time------------------------------------"+(fastJsonEnd-fastJsonBegin));
        //jackson
        ObjectMapper jackson = new ObjectMapper();
        long jacksonBegin =  System.currentTimeMillis();

        try {
            System.out.println("jackson-------------------------------------------------");
            //System.out.println("jackson"+jackson.writeValueAsString(map));
            jackson.writeValueAsString(map);
            long jacksonEnd =  System.currentTimeMillis();
            System.out.println("jackson-------------time------------------------------------"+(jacksonEnd-jacksonBegin));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }

    public static Map getMapData(Map map,int count) {
        for (int i = 0;i < count;i++) {
            map.put("a"+i,i);
        }
        return map;
    }

运行结果:

通过测试发现fastjson运行速度更胜一筹。

但最近几年fastjson的漏洞比较多,大家使用的时候注意。

posted @ 2022-06-13 10:31  星星之草%  阅读(277)  评论(2编辑  收藏  举报