JAvaシーケンス化オブジェクト時間、サイズ比較
3355 ワード
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<loop; 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<loop; i++) {
byte[] b = userInfo.codeC(buffer);
}
endTime = System.currentTimeMillis();
System.out.println("The byte array Serializable cost time is : "+ (endTime-startTime) + "ms");
}
}