fastjson和jackson的简单对比

本文通过简单的实验对比了FastJSON和Jackson两个流行的Java JSON处理库的性能表现。实验使用了1000条、5000条及10000条数据进行测试,并记录了各自所需的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

中午吃完饭无聊,做了个fastjson和jackson的简单对比。fastjson是阿里做的国有开源Java工具包,jackson是spring mvc内置的json转换工具,孰强孰弱呢?结果吓我一跳!后面三张图,分别是1000条数据、5000条和1W条!

注:年轻时写着玩儿的,代码逻辑混乱,有严重bug。大家图个乐好了。
标签:  fastjson  Jackson

代码片段(6)[全屏查看所有代码]

1. [代码]主测试程序     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class JsonParseTest {
 
     public static void main(String[] args) throws JsonProcessingException {
         Monitoring.begin();
         List<Corp> list = Lists.newArrayList();
         for ( int i = 0 ; i < 1000 ; i++) {
             list.add(fullObject(Corp. class ));
         }
         Monitoring.end( "生成数据" );
 
         Monitoring.begin();
         jackson(list);
         Monitoring.end( "Jackson" );
 
         Monitoring.begin();
         fastjson(list);
         Monitoring.end( "fastjson" );
 
     }
 
     public static void fastjson(List<Corp> list) {
         for (Corp corp : list) {
             String string = JSON.toJSONString(corp);
         }
     }
 
     public static void jackson(List<Corp> list) throws JsonProcessingException {
         for (Corp corp : list) {
             String string = new ObjectMapper().writeValueAsString(corp);
         }
     }
 
     /**
      * 填充一个对象(一般用于测试)
      */
     public static <T> T fullObject(Class<T> cl) {
         T t = null ;
         try {
             t = cl.newInstance();
             Method methods[] = cl.getMethods();
             for (Method method : methods) {
                 // 如果是set方法,进行随机数据的填充
                 if (method.getName().indexOf( "set" ) == 0 ) {
                     Class param = method.getParameterTypes()[ 0 ];
                     if (param.equals(String. class )) {
                         method.invoke(t, randomCodes( 5 ));
                     } else if (param.equals(Short. class )) {
                         method.invoke(t, ( short ) new Random().nextInt( 5 ));
                     } else if (param.equals(Float. class )) {
                         method.invoke(t, new Random().nextFloat());
                     } else if (param.equals(Double. class )) {
                         method.invoke(t, new Random().nextDouble());
                     } else if (param.equals(Integer. class )) {
                         method.invoke(t, new Random().nextInt( 10 ));
                     } else if (param.equals(Long. class )) {
                         method.invoke(t, new Random().nextLong());
                     } else if (param.equals(Date. class )) {
                         method.invoke(t, new Date());
                     } else if (param.equals(Timestamp. class )) {
                         method.invoke(t, new Timestamp(System.currentTimeMillis()));
                     } else if (param.equals(java.sql.Date. class )) {
                         method.invoke(t, new java.sql.Date(System.currentTimeMillis()));
                     }
                 }
             }
         } catch (InstantiationException e) {
             e.printStackTrace();
         } catch (IllegalAccessException e) {
             e.printStackTrace();
         } catch (IllegalArgumentException e) {
             e.printStackTrace();
         } catch (InvocationTargetException e) {
             e.printStackTrace();
         }
         return t;
     }
}

2. [代码]Corp类     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Corp {
     private Long uid;
     private Integer corpGrade;
     private Integer cityId;
     private String name;
     private String EName;
     private String description;
     private String zipCode;
     private String tel;
     private String fax;
     private String EMail;
     private Integer isEmailOpen;
     private Integer EMailChecked;
     private Timestamp regDateOnGov;
     private String backroll;
     private String address;
     private String webStoreUrl;
     private Integer isNew;
     private Integer credit;
     private Integer activeDegrees;
     private Integer hits;
     private Integer isHitsRecord;
     private Timestamp regTimeOnZfa;
     private Integer corpType;
     private Integer corpMajorcategoryId;
     private Integer businessRoleId;
     private String keyword;
     private Integer developState;
     private String isAlert;
     private Integer advMemState;
     private Integer advStockState;
     private Integer allianceState;
     private Timestamp lastUpdateTime;
     private Integer corpMajorcategoryId1;
     private String keyword1;
     private Long certificatePic;
     private Integer isUpdateCharter;
     private Integer currcount;
     private Integer curronsale;
     private Integer curronhot;
     private Integer currniccount;
     private Integer currniconsale;
     private Integer currniconhot;
     private String buyProducts;
     private Integer isOpenShop;
     private Integer state;
     private String mainProduct;
     private String advBrandIds;
     private String feature;
     private Integer category;
     private Integer contactFlag;
     private String fastPassage;
     
     //省略getter、setter方法
}

3. [代码]小的监控管理类     

?
1
2
3
4
5
6
7
8
9
10
11
12
public class Monitoring {
     private static ThreadLocal<Long> begin = new ThreadLocal<Long>();
 
     public static void begin() {
         begin.set(System.currentTimeMillis());
     }
 
     public static void end(String name) {
         double time = (System.currentTimeMillis() - begin.get()) / 1000.0 ;
         System.out.println(name + "所用时间(秒):" + time);
     }
}

4. [图片] 1000.png    

5. [图片] 5000.png    

6. [图片] 10000.png    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值