码迷,mamicode.com
首页 > Web开发 > 详细

小测一下fastjson的速度

时间:2015-10-29 16:35:11      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

    fastjson确实很厉害(毕竟是阿里大牛写的),在逻辑解析的快捷上可以坐到很方便的搞定,这样直接就直接缩小的很多的工作量,并且代码结构很清晰。

    基于以上摘要内容,我就闲着无聊比较了一下。代码如下

    

 public void testFastJson(){

   String jsonString =   "[\n" +
              "{\n" +
              "orderId: 2964,\n" +
              "code: \"2800001296442\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 22400,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-28 18:28:42\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"小黄瓜,无籽瓜\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2940,\n" +
              "code: \"2700001294044\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 650,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-27 20:05:44\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"两个装柠檬\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2913,\n" +
              "code: \"2600001291319\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 72500,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-26 20:56:19\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"韩国梨,纸箱白心柚,小黄瓜,野生苹果\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2891,\n" +
              "code: \"2500001289120\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 9000,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-25 21:06:20\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"哈密瓜(4个装)\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2865,\n" +
              "code: \"2400001286555\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 81100,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-24 19:57:55\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"西梅,哈密瓜(4个装),云南蜜桔,小黄瓜,木瓜,无籽瓜\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2839,\n" +
              "code: \"2300001283923\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 69000,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-23 20:50:23\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"哈密瓜(4个装),有机红心柚,凯特芒果\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2820,\n" +
              "code: \"2200001282039\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 37500,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-22 20:43:39\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"马奶提,哈密瓜(4个装),小黄瓜\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2792,\n" +
              "code: \"2100001279242\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 65500,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-21 22:11:42\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"菲律宾凤梨,辽宁葡萄,小黄瓜,野生苹果\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2773,\n" +
              "code: \"2000001277334\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 68800,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-20 22:14:34\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"菲律宾香蕉,无籽瓜,野生苹果\"\n" +
              "},\n" +
              "{\n" +
              "orderId: 2737,\n" +
              "code: \"1900001273702\",\n" +
              "radiateCenterId: 1,\n" +
              "userId: 7,\n" +
              "totalMoney: 101000,\n" +
              "status: 7,\n" +
              "payStatus: 2,\n" +
              "sendStatus: 2,\n" +
              "payway: 1,\n" +
              "gmtCreate: \"2015-10-19 21:35:02\",\n" +
              "shopName: \"水果无忧\",\n" +
              "itemsName: \"韩国梨,猕猴桃金果\"\n" +
              "}\n" +
              "]";
              
    //这是待解析的json字符串。
    
    long t1,t2;
    t1 =System.currentTimeMillis();
    List<MyOrderBean> list = JSONArray.parseArray(jsonString, MyOrderBean.class);
    t2 =System.currentTimeMillis();

    logError("fastjson  = " + (t2 - t1));

}

    

    以及自己的解析代码:

public class OrderHelper {

   public static long t1,t2;

   public static List<MyOrderBean> getOrderList(JSONObject object){

      t1 =System.currentTimeMillis();

      JSONArray array = JsonUtil.getJsonArray(object, "data");
      List<MyOrderBean> list = CollectionUtil.newArrayList();
      for (int i = 0; i < array.length(); i++) {
         try {
            JSONObject obj = JsonUtil.getJsonObject(array.get(i));
            list.add(getOrder(obj));
         } catch (JSONException e) {
            e.printStackTrace();
         }
      }
      t2 =System.currentTimeMillis();

      LogUtil.logTagE("my useTime = " + (t2 - t1));

      return list;
   }
   
   public static MyOrderBean getOrder(JSONObject obj){
      MyOrderBean orders = new MyOrderBean();

      orders.setId(JsonUtil.getInt(obj, "orderId", 0));
      orders.setCode(JsonUtil.getString(obj, "code", ""));
      orders.setGmtCreate(JsonUtil.getString(obj, "gmtCreate", ""));
      orders.setItemName(JsonUtil.getString(obj, "itemsName", ""));
      orders.setItemNum(JsonUtil.getInt(obj, "itemNum", 0));
      orders.setPayWay(JsonUtil.getInt(obj, "payway", 0));
      orders.setShopName(JsonUtil.getString(obj, "shopName", ""));
      orders.setStatus(JsonUtil.getInt(obj, "status", 0));
      orders.setShopId(JsonUtil.getInt(obj, "shopId", 0));
      orders.setTotalMoney(PriceUtil.showPriceYang( JsonUtil.getDouble(obj, "totalMoney", 0.0d)));
      orders.setPayStatus(JsonUtil.getInt(obj, "payStatus", 1));
      orders.setSendStatus(JsonUtil.getInt(obj, "sendStatus", 1));
      
      return orders;
   }
}


    运行结果如下:

技术分享

    结论如上、差距比较大。。。看到这个结果我就心安了,不说了我要去嘲讽我同学了。


    其实总结了一下我不用fastjson的原因如下:

    第一:每个项目,服务端的数据是多变的,根据不同的界面,服务端传回的数据是不一样的,这样就导致你整个实体里面可能有很多属性不是当前页面用得到的,fastjson的解析原理是通过反射找到所有get方法,于是再逐个比较解析。,使用fastjson解析无疑会多此一举。这可能是一个耗时长的原因,毕竟有很多无用的属性。

    第二 :习惯自己写解析类,更加方便精确的针对每一个实体做解析,主要也是我自己写习惯了,闭着眼睛也知道怎么写,不过我这样有点小不方便,就是换一家公司之后,新的网络规范必须要重新适应,fastjson使用频率广泛我想也是有这点原因在里面。毕竟不用动脑直接一波流。

====================================分割线================================

   

    好久没写博客了,毕业两年来,做合伙人这么长时间,经历了很多艰难的时期,挺过了好几个,什么都干过,运营,产品,市场地推,物流跟车,仓库分拣,累的不要不要的,技术成长却很少,看书时间减少,个人安排时间不足,上周末跟网易的同学聊天扯淡,真心羡慕一心写代码的生活。大企业不管有多么多的诟病沉珂,但是不可否认那里聚集了很多各自领域都很优秀的人才,圈子决定眼界,至少技术上可以帮助更加快速成长,这是我在预感项目失败公司清算的时候自己应该怎么选择的感悟。最近几个月是公司从未有过的艰难时期,这一次,可能坚持不下去了。毕竟太年轻,积累太少,也因为年轻,可以多尝试几条路。

    很多年轻人(我自己也是)不喜欢大公司的环境,不习惯大公司的相处方式,整天吐槽臃肿不堪甚至浪费资源的公司结构和制度网上一直在讨论的,去大公司还是去小公司,大神们有无数的结果和比较,我小小平凡人一枚就不在此浪费字数了。你选择什么样的工作,什么样的环境,什么样规模的平台,优劣都有, 其实在我看来都无所谓,别忘了自己还年轻,走错路可以回头,就算不想回头也可以继续往下走。所以只要肯走,没什么可怕的。

    共勉。


小测一下fastjson的速度

标签:

原文地址:http://my.oschina.net/halfront/blog/523585

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!