自定义序列化和JDK序列化比较

实体类

自定义实体类,其中实现了自定义的两种序列化方式,原理就是将对象转为二进制。

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 userName;
    }


    public final void setUserName(String userName) {
		this.userName = userName;
    }


    public final int getUserID() {
		return userID;
    }


    public final void setUserID(int userID) {
		this.userID = userID;
    }

    //自行序列化
    public byte[] codeC() {
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		byte[] value = this.userName.getBytes();//userName转换为字节数组value
		buffer.putInt(value.length);//写入字节数组value的长度
		buffer.put(value);//写入字节数组value的值
		buffer.putInt(this.userID);//写入userID的值
		buffer.flip();//准备读取buffer中的数据
		value = null;
		byte[] result = new byte[buffer.remaining()];
		buffer.get(result);//buffer中的数据写入字节数组并作为结果返回
		return result;
    }

	//自行序列化方法2
    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;
    }
}

测试序列化后字节大小

/**
 * 类说明:测试序列化后字节大小
 */
public class TestUserInfo {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
		UserInfo info = new UserInfo();
		info.buildUserID(100).buildUserName("Welcome to Netty");
		//使用jdk的序列化
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream os = new ObjectOutputStream(bos);
		os.writeObject(info);
		os.flush();
		os.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 : "
			+ info.codeC().length);
    }

}

结果:
在这里插入图片描述
可以看出,jdk序列化后的字节长度明显大于自定义的长度。

测试序列化性能差异

/**
 * 类说明:测试序列化性能差异
 */
public class PerformTestUserInfo {

    public static void main(String[] args) throws IOException {
        UserInfo info = new UserInfo();
        info.buildUserID(100).buildUserName("Welcome to Netty");
        int loop = 1000000;

        //使用jdk的序列化
        ByteArrayOutputStream bos = null;
        ObjectOutputStream os = null;
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < loop; i++) {
            bos = new ByteArrayOutputStream();
            os = new ObjectOutputStream(bos);
            os.writeObject(info);
            os.flush();
            os.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 = info.codeC(buffer);
        }
        endTime = System.currentTimeMillis();
        System.out.println("The byte array serializable cost time is : "
            + (endTime - startTime) + " ms");

        }

}

结果:
在这里插入图片描述
可以看出jdk的运行时间是自定义的18倍左右。

总结

jdk的序列化方式虽然很简单,但是效率很低。

猜你喜欢

转载自blog.csdn.net/qq_28822933/article/details/83626547