msgpack-java
msgpack-java copied to clipboard
Why MessagePackMapper.writeValueAsBytes() is slower than ObjectMapper.writeValueAsString()?
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() ??