标签:使用jdk序列化 和 二进制编解码 从执行时间 产生的字节大小作对比
实例代码 : http://download.csdn.net/detail/jiangtao_st/7677503
/** * * <p> *用户实体对象定义 * </p> * * @author 卓轩 * @创建时间:2014年6月20日 * @产品: UIC * @version: V1.0 */ public class UserDO implements Serializable { private static final long serialVersionUID = 7289036533757178921L; /** * userid */ private int userId; /** * email */ private String email; /** * uname */ private String uname; /** * unick */ private String unick; /** * sex */ private Integer sex; public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getUnick() { return unick; } public void setUnick(String unick) { this.unick = unick; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } @Override public String toString() { return "UserDO ["+this.userId+"],["+this.uname+"],["+this.unick+"],["+this.email+"]"; } public byte [] coder(){ ByteBuffer buffer = ByteBuffer.allocate(1024); byte [] email = this.email.getBytes(); buffer.putInt(email.length); buffer.put(email); byte [] uname =this.uname.getBytes(); buffer.putInt(uname.length); buffer.put(uname); byte [] unick = this.unick.getBytes(); buffer.putInt(unick.length); buffer.put(unick); buffer.putInt(this.sex); buffer.putInt(this.userId); buffer.flip(); byte[] result = new byte[buffer.remaining()]; buffer.get(result); return result; } }
/** * * <p> * 对比对象编解码 优劣 * * 代码通过从同一个对象,使用jdk序列化 和 二进制编解码 ,从执行时间,产生的字节大小作对比 * </p> * * @author 卓轩 * @创建时间:2014年7月8日 * @version: V1.0 */ public class ObjectCoderCompare { /** * 对比序列化文件大小 */ @Test public void sizeByteCompare(){ try { UserDO zhuoxuan = new UserDO(); zhuoxuan.setUserId(113445); zhuoxuan.setSex(1); zhuoxuan.setUname("卓轩"); zhuoxuan.setUnick("zhuoxuan"); zhuoxuan.setEmail("zhuoxuan@mogujie.com"); System.out.println("byte array 序列化大小:" + zhuoxuan.coder().length); //jdk 序列化对象 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos); objectOutputStream.writeObject(zhuoxuan); objectOutputStream.flush(); objectOutputStream.close(); byte[] byteArray = bos.toByteArray(); System.out.println("jdk序列化对象大小:" + byteArray.length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 执行时间,性能对比 */ @Test public void performCompare(){ try { UserDO zhuoxuan = new UserDO(); zhuoxuan.setUserId(113445); zhuoxuan.setSex(1); zhuoxuan.setUname("卓轩"); zhuoxuan.setUnick("zhuoxuan"); zhuoxuan.setEmail("zhuoxuan@mogujie.com"); long startTime = System.currentTimeMillis(); int count = 1000000; for (int i = 0; i < count; i++) { zhuoxuan.coder(); } long endTime = System.currentTimeMillis(); System.out.println("byte array 执行时间:" + (endTime-startTime) + "ms"); startTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { //jdk 序列化对象 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(bos); objectOutputStream.writeObject(zhuoxuan); objectOutputStream.flush(); objectOutputStream.close(); byte[] byteArray = bos.toByteArray(); } endTime = System.currentTimeMillis(); System.out.println("jdk 序列化对象 执行时间:" + (endTime-startTime) + "ms"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
byte array 序列化大小:54
jdk序列化对象大小:267
Java网络编程-对象编解码方案、优劣对比,布布扣,bubuko.com
标签:使用jdk序列化 和 二进制编解码 从执行时间 产生的字节大小作对比
原文地址:http://blog.csdn.net/jiangtao_st/article/details/38117739