今天测试序列化对象的时候循环10000次下列对象 protostuff报错
而java的序列化不但没报错而且速度一直比proto快
下面是实体类,和具体序列化方法
java原生序列化是最稳定的,我用过kryo,执行10000次的效率比java原生的好,但是多了会有不知名的错误
<spanstyle="color:#000080;font-weight:bold;">intsize=<spanstyle="color:#0000ff;">100;Map<String,String>childMap=<spanstyle="color:#000080;font-weight:bold;">newHashMap<String,String>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){childMap.put(i+<spanstyle="color:#008000;font-weight:bold;">"",<spanstyle="color:#008000;font-weight:bold;">"mapvalue:"+i);}List<String>chilList=<spanstyle="color:#000080;font-weight:bold;">newArrayList<String>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){chilList.add(<spanstyle="color:#008000;font-weight:bold;">"listvalue:"+i);}SerializerEntityChildchildEntity=<spanstyle="color:#000080;font-weight:bold;">newSerializerEntityChild();childEntity.setBoolvalue(<spanstyle="color:#000080;font-weight:bold;">true);childEntity.setMap(childMap);childEntity.setList(chilList);List<SerializerEntityChild>childList=<spanstyle="color:#000080;font-weight:bold;">newArrayList<SerializerEntityChild>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){childEntity.setIntvalue(i);childEntity.setLongvalue(i);childEntity.setKey(<spanstyle="color:#008000;font-weight:bold;">"key:"+i);childList.add(childEntity);}SerializerEntityentity=<spanstyle="color:#000080;font-weight:bold;">newSerializerEntity();Map<String,SerializerEntityChild>map=<spanstyle="color:#000080;font-weight:bold;">newHashMap<String,SerializerEntityChild>();<spanstyle="color:#000080;font-weight:bold;">for(<spanstyle="color:#000080;font-weight:bold;">inti=<spanstyle="color:#0000ff;">0;i<size;i++){childEntity.setIntvalue(i);childEntity.setLongvalue(i);childEntity.setKey(<spanstyle="color:#008000;font-weight:bold;">"key:"+i);map.put(i+<spanstyle="color:#008000;font-weight:bold;">"",childEntity);}entity.setList(childList);entity.setMap(map);entity.setKey(<spanstyle="color:#008000;font-weight:bold;">"main");entity.setBoolvalue(<spanstyle="color:#000080;font-weight:bold;">true);entity.setIntvalue(<spanstyle="color:#0000ff;">1);
entity.setLongvalue(<spanstyle="color:#0000ff;">1);
Schema<SerializerEntity>schemal=RuntimeSchema.<spanstyle="font-style:italic;">createFrom(SerializerEntity.<spanstyle="color:#000080;font-weight:bold;">class);st=System.<spanstyle="font-style:italic;">currentTimeMillis();<spanstyle="color:#000080;font-weight:bold;">byte[]prob=ProtostuffIOUtil.<spanstyle="font-style:italic;">toByteArray(entity,schemal,LinkedBuffer.<spanstyle="font-style:italic;">allocate(LinkedBuffer.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">MIN_BUFFER_SIZE));System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"序列化大小:"+prob.<spanstyle="color:#660e7a;font-weight:bold;">length);System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"用时--------------------"+(System.<spanstyle="font-style:italic;">currentTimeMillis()-st));System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"反序列化------------------");st=System.<spanstyle="font-style:italic;">currentTimeMillis();SerializerEntitynewent=<spanstyle="color:#000080;font-weight:bold;">newSerializerEntity();ProtostuffIOUtil.<spanstyle="font-style:italic;">mergeFrom(prob,newent,schemal);System.<spanstyle="color:#660e7a;font-weight:bold;font-style:italic;">out.println(<spanstyle="color:#008000;font-weight:bold;">"用时--------------------"+(System.<spanstyle="font-style:italic;">currentTimeMillis()-st));
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。