fastjson初始化对性能的影响(转)

之前在项目中序列化是用thrift,性能一般,而且需要用编译器生成新的类,在序列化和反序列化的时候感觉很繁琐,因此想转到json阵营。对比了jackson,gson等框架之后,决定用fastjson,为什么呢,因为看名字感觉很快。。。

网上的说法:

fastjson 是一个性能很好的 Java 语言实现的 JSON 解析器和生成器,来自阿里巴巴的工程师开发。
主要特点:
快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson) 强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum) 零依赖(没有依赖其它任何类库除了JDK)

但是测试的结果让我大跌眼镜。

Test Case: 对一个User类(空)的对象分别用java和fastjson序列化1000次,再反序列化1000次,计算时间。注: 测试不是非常严谨,只是做个简单的比较。

Test Result: 

Type = java Serialize cost = 27ms Deserialize cost = 75ms
Type = fastjson Serialize cost = 385ms Deserialize cost = 84ms

这是在逗我吗。。。

经过同事提醒,看了源码发现fastjson在序列化时需要初始化SerializeConfig,反序列化时需要初始化ParserConfig。然后我在测试案例中加入这两句

public static ParserConfig pc = new ParserConfig();
public static SerializeConfig sc = new SerializeConfig();

果然快了很多,但仍旧不理想

Type = fastjson Serialize cost = 36ms Deserialize cost = 42ms

再继续看,发现还需要初始化writer和parser,所以改成这句

JSON.parseObject(JSON.toJSONString(new User()), User.class);

结果就很令人满意了

Type = fastjson Serialize cost = 15ms Deserialize cost = 18ms

结论: 如果你使用fastjson在一个短进程,换句话说只是少量的进行序列化反序列化,那么fastjson由于初始化需要的时间比较长,总体性能将会很糟糕。如果一定要用,有必要的话可以考虑手动进行初始化。


另,补上测试代码:

class User implements Serializable {
    private static final long serialVersionUID = -2513747641863637392L;

    User() {
    }
}

public class Test {
//    public static ParserConfig pc = new ParserConfig();
//    public static SerializeConfig sc = new SerializeConfig();

    public static void main(String[] args) throws UnknownHostException {
//        JSON.parseObject(JSON.toJSONString(new User()), User.class);
        String type = "json";
        System.out.println("Type = " + type);
        long start = new Date().getTime();
        byte[] b = serialize(new User(), type);
        long mid = new Date().getTime();
        System.out.println("Serialize cost = " + (mid - start));
        deserialize(b, type);
        System.out.println("Deserialize cost = " + (new Date().getTime() - mid));
    }

    public static byte[] serialize(User user, String type) {
        byte[] b = null;
        for (int i = 0; i < 1000; i++) {
            if ("java".equalsIgnoreCase(type)) {
                b = javaSerialize(user);
            } else if ("json".equalsIgnoreCase(type)) {
                b = jsonSerialize(user);
            }
        }
        return b;
    }

    public static User deserialize(byte[] b, String type) {
        User user = null;
        for (int i = 0; i < 1000; i++) {
            if ("java".equalsIgnoreCase(type)) {
                user = javaDeserialize(b);
            } else if ("json".equalsIgnoreCase(type)) {
                user = jsonDeserialize(b);
            }
        }
        return user;
    }

    public static byte[] jsonSerialize(User user) {
        return JSON.toJSONString(user).getBytes();
    }

    public static User jsonDeserialize(byte[] b) {
        return JSON.parseObject(new String(b), User.class);
    }

    public static byte[] javaSerialize(User user) {
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream os = new ObjectOutputStream(out);
            os.writeObject(user);
            return out.toByteArray();
        } catch (Exception e) {
            throw new SerializationException(e);
        }
    }

    public static User javaDeserialize(byte[] b) {
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(b);
            ObjectInputStream is = new ObjectInputStream(in);
            return (User) is.readObject();
        } catch (Exception e) {
            throw new SerializationException(e);
        }
    }
}

http://kane-xie.iteye.com/blog/2223837






 

 

原文地址:https://www.cnblogs.com/softidea/p/4617433.html