msgpack-java icon indicating copy to clipboard operation
msgpack-java copied to clipboard

Why MessagePackMapper.writeValueAsBytes() is slower than ObjectMapper.writeValueAsString()?

Open yswang0927 opened this issue 3 years ago • 0 comments

public static void main(String[] args) throws Exception {

        User u1 = new User("张三", 20);
        u1.addAddress(new Address("南京市", "210010"));
        u1.addAddress(new Address("苏州市", "310012"));
        u1.addAddress(new Address("宿迁市", "223800"));
        for (int i = 1; i <= 1000000; i++) {
            u1.addAddress(new Address("因为在前后端分离的开发模式下,对于前端开发人员,是不应该关注后端的技术实现。"+ i, "56878"+ i));
        }

        ObjectMapper objectMapper = new MessagePackMapper();
        long stime = System.currentTimeMillis();
        byte[] bytes = objectMapper.writeValueAsBytes(u1);
        System.out.println(">>> msgpack cost:"+ (System.currentTimeMillis() - stime) + " ms");

        ObjectMapper obm = new ObjectMapper();
        stime = System.currentTimeMillis();
        String str = obm.writeValueAsString(u1);
        System.out.println(">>> to json string cost:"+ (System.currentTimeMillis() - stime) + " ms");

    }

Test above code:

msgpack cost:935 ms to json string cost:539 ms

Why MessagePackMapper.writeValueAsBytes() is slower than ObjectMapper.writeValueAsString() ??

yswang0927 avatar Oct 11 '22 09:10 yswang0927