简体   繁体   English

为什么杰克逊这么慢?

[英]Why Jackson is so slow?

I use simple Jackson code to convert from my object to json String, but it really slow.我使用简单的 Jackson 代码从我的对象转换为 json 字符串,但它真的很慢。 It takes 70 milisecond to convert only one object as below in my machine.在我的机器中只转换一个对象需要 70 毫秒。 Did I do something wong?我做了什么吗?

ObjectMapper myObjectMapper = new ObjectMapper();
void testJson()
{

    MyClass state = new MyClass();
    try
    {
        String result = myObjectMapper.writeValueAsString(state);
    } catch (Exception ex)
    {

    }

}

MyClass with only 4 members MyClass 只有 4 名成员

 MyClass
    {
    public int a;
        public  int b;
        public int c;
        public String d;
    }

I use simple Jackson code to convert from my object to json String, but it really slow.我使用简单的 Jackson 代码从我的对象转换为 json 字符串,但它真的很慢。 It takes 70 milisecond to convert only one object as below in my machine.在我的机器中只转换一个对象需要 70 毫秒。 Did I do something wong?我做了什么吗?

70 milliseconds to encode and send a JSON message is implausible. 70 毫秒来编码和发送 JSON 消息是难以置信的。 There is little doubt in my mind that the real explanation is that the 70 millisecond measurement an artefact of the way that you benchmarked your code.毫无疑问,真正的解释是 70 毫秒的测量是您对代码进行基准测试的方式的产物。 Probably, you didn't allow for JVM warm-up effects.可能,您没有考虑 JVM 预热效果。

So, yes, you did your benchmarking incorrectly.所以,是的,你做的基准测试不正确。 Probably.大概。

Now I found out three solutions for sending MyClass's instance over network: 1: use Jackson to convert into byte[] and send.现在我找到了通过网络发送MyClass的实例的三种解决方案: 1:使用Jackson转换为byte[]并发送。 2: use built-in serialization to convert into byte[] and send. 2:使用内置序列化转换成byte[]并发送。 3: convert MyClass's members into String, then into byte[] and send. 3:将MyClass的成员转成String,再转成byte[]发送。 Is there any other better solution ?还有其他更好的解决方案吗?

In theory (ie if you have enough skill, time and patience) the best performance can be achieved by encoding the data by hand into a byte buffer and sending that.理论上(即,如果您有足够的技巧、时间和耐心)可以通过手动将数据编码到字节缓冲区并发送它来实现最佳性能。 But that is only theoretical.但这只是理论上的。

If you are looking for practical solutions that are potentially faster than the alternatives you have tried, take a look at Google Protocol Buffers .如果您正在寻找可能比您尝试过的替代方案更快的实用解决方案,请查看Google Protocol Buffers They are reputed to be fast ...他们被认为是快速...


70 milliseconds for one object, but when I try 1000 objects, it only takes 114 milliseconds, isn't it strange?一个对象70毫秒,但是当我尝试1000个对象时,只需要114毫秒,是不是很奇怪?

Actually, it is not at all strange when you take account of the way that the JVM works.实际上,考虑到 JVM 的工作方式,这并不奇怪。

When the JVM starts, it loads your code and starts running it using the bytecode interpreter.当 JVM 启动时,它会加载您的代码并开始使用字节码解释器运行它。 After the code has been interpreted for a bit, the JVM runs the JIT compiler to produce optimized native code for the methods that are being called frequently.在对代码进行一点解释之后,JVM 运行 JIT 编译器,为频繁调用的方法生成优化的本机代码。 This compilation process takes a significant amount of time.此编译过程需要大量时间。

So when you are measuring the time taken to send one object, you are probably really measuring the time to send one object AND do a bunch of JIT compilation.因此,当您测量发送一个对象所花费的时间时,您可能实际上是在测量发送一个对象的时间并进行大量 JIT 编译。 But that JIT compilation work won't need to repeated.但是不需要重复 JIT 编译工作。 Net result - processing one object appears to takes a surprising long time, compared to 1000.最终结果 - 与 1000 个相比,处理一个对象似乎需要很长时间。

JIT compilation is one of the common JVM warmup effects that can distort a poorly written Java benchmark. JIT 编译是常见的 JVM 预热效果之一,它会扭曲编写不佳的 Java 基准测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM