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

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

 

       之前在项目中序列化是用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。然后我在测试案例中加入这两句

 

Java代码   收藏代码
  1. public static ParserConfig pc = new ParserConfig();  
  2. public static SerializeConfig sc = new SerializeConfig();  

 

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

 

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

 

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

Java代码   收藏代码
  1. JSON.parseObject(JSON.toJSONString(new User()), User.class);  

 

结果就很令人满意了

 

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

 

 

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

 

另,补上测试代码:

Java代码   收藏代码
  1. class User implements Serializable {  
  2.     private static final long serialVersionUID = -2513747641863637392L;  
  3.   
  4.     User() {  
  5.     }  
  6. }  
  7.   
  8. public class Test {  
  9. //  public static ParserConfig pc = new ParserConfig();  
  10. //  public static SerializeConfig sc = new SerializeConfig();  
  11.   
  12.     public static void main(String[] args) throws UnknownHostException {  
  13. //      JSON.parseObject(JSON.toJSONString(new User()), User.class);  
  14.         String type = "json";  
  15.         System.out.println("Type = " + type);  
  16.         long start = new Date().getTime();  
  17.         byte[] b = serialize(new User(), type);  
  18.         long mid = new Date().getTime();  
  19.         System.out.println("Serialize cost = " + (mid - start));  
  20.         deserialize(b, type);  
  21.         System.out.println("Deserialize cost = " + (new Date().getTime() - mid));  
  22.     }  
  23.   
  24.     public static byte[] serialize(User user, String type) {  
  25.         byte[] b = null;  
  26.         for (int i = 0; i < 1000; i++) {  
  27.             if ("java".equalsIgnoreCase(type)) {  
  28.                 b = javaSerialize(user);  
  29.             } else if ("json".equalsIgnoreCase(type)) {  
  30.                 b = jsonSerialize(user);  
  31.             }  
  32.         }  
  33.         return b;  
  34.     }  
  35.   
  36.     public static User deserialize(byte[] b, String type) {  
  37.         User user = null;  
  38.         for (int i = 0; i < 1000; i++) {  
  39.             if ("java".equalsIgnoreCase(type)) {  
  40.                 user = javaDeserialize(b);  
  41.             } else if ("json".equalsIgnoreCase(type)) {  
  42.                 user = jsonDeserialize(b);  
  43.             }  
  44.         }  
  45.         return user;  
  46.     }  
  47.   
  48.     public static byte[] jsonSerialize(User user) {  
  49.         return JSON.toJSONString(user).getBytes();  
  50.     }  
  51.   
  52.     public static User jsonDeserialize(byte[] b) {  
  53.         return JSON.parseObject(new String(b), User.class);  
  54.     }  
  55.   
  56.     public static byte[] javaSerialize(User user) {  
  57.         try {  
  58.             ByteArrayOutputStream out = new ByteArrayOutputStream();  
  59.             ObjectOutputStream os = new ObjectOutputStream(out);  
  60.             os.writeObject(user);  
  61.             return out.toByteArray();  
  62.         } catch (Exception e) {  
  63.             throw new SerializationException(e);  
  64.         }  
  65.     }  
  66.   
  67.     public static User javaDeserialize(byte[] b) {  
  68.         try {  
  69.             ByteArrayInputStream in = new ByteArrayInputStream(b);  
  70.             ObjectInputStream is = new ObjectInputStream(in);  
  71.             return (User) is.readObject();  
  72.         } catch (Exception e) {  
  73.             throw new SerializationException(e);  
  74.         }  
  75.     }  
  76. }  

 

猜你喜欢

转载自xhmj12.iteye.com/blog/2223872