java 对象序列化 大小_java序列化对象时间、大小比较

import java.io.Serializable;

import java.nio.ByteBuffer;

public class UserInfo implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

private String userName;

private int userID;

public UserInfo buildUserName(String userName) {

this.userName = userName;

return this;

}

public UserInfo buildUserID(int userID) {

this.userID = userID;

return this;

}

public final String getUserName() {

return this.userName;

}

public final void setUserName(String userName) {

this.userName = userName;

}

public final int getUserID() {

return this.userID;

}

public final void setUserID(int userID) {

this.userID = userID;

}

public byte[] codec() {

ByteBuffer buffer = ByteBuffer.allocate(1024);

byte[] value = this.userName.getBytes();

buffer.putInt(value.length);

buffer.put(value);

buffer.putInt(this.userID);

buffer.flip();

value = null;

byte[] result = new byte[buffer.remaining()];

buffer.get(result);

return result;

}

public byte[] codeC(ByteBuffer buffer) {

buffer.clear();

byte[] value = this.userName.getBytes();

buffer.putInt(value.length);

buffer.put(value);

buffer.putInt(this.userID);

buffer.flip();

value = null;

byte[] result = new byte[buffer.remaining()];

buffer.get(result);

return result;

}

}

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectOutputStream;

import java.nio.ByteBuffer;

public class TestUserInfo {

public static void main(String[] args) throws IOException {

//testA();

testB();

}

public static void testA() throws IOException {

UserInfo userInfo = new UserInfo();

userInfo.buildUserID(100).buildUserName("Welcome to Netty");

ByteArrayOutputStream bos = new ByteArrayOutputStream();

ObjectOutputStream oos = new ObjectOutputStream(bos);

oos.writeObject(userInfo);

oos.flush();

oos.close();

byte[] b = bos.toByteArray();

System.out.println("The jdk Serializable length is : "+b.length);

bos.close();

System.out.println("-------------------------------------------------");

System.out.println("The byte array Serializable length is :"+userInfo.codec().length);

}

public static void testB() throws IOException {

UserInfo userInfo = new UserInfo();

userInfo.buildUserID(100).buildUserName("Welcome to Netty");

int loop = 1000000;

ByteArrayOutputStream bos = null;

ObjectOutputStream oos = null;

long startTime = System.currentTimeMillis();

for(int i=0; i

bos = new ByteArrayOutputStream();

oos = new ObjectOutputStream(bos);

oos.writeObject(userInfo);

oos.flush();

oos.close();

byte[] b = bos.toByteArray();

bos.close();

}

long endTime = System.currentTimeMillis();

System.out.println("The jdk Serializable cost time is : "+ (endTime-startTime) + "ms");

System.out.println("-------------------------------------------------");

ByteBuffer buffer = ByteBuffer.allocate(1024);

startTime = System.currentTimeMillis();

for(int i=0; i

byte[] b = userInfo.codeC(buffer);

}

endTime = System.currentTimeMillis();

System.out.println("The byte array Serializable cost time is : "+ (endTime-startTime) + "ms");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值