Java序列化对象成文件的效率对比

一、Kryo

  写入文件:

public static <T>  boolean serializeFile(T obj,String path) {
        if(null != obj){
            Output output=null;
            try {
                File outFile = new File(path);
                if(!outFile.getParentFile().exists()){
                    outFile.getParentFile().mkdirs();
                }
                Kryo kryo = kryos.get();
                output = new Output(new FileOutputStream(path));
                kryo.writeObject(output, obj);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                close(output);
            }
        }
        return false;
    }

读取文件:

public static <T> T unSerializeFile(String path,Class<T> t) {
        if(null != path && null !=t ){
            Input input=null;
            try {
                Kryo kryo = kryos.get();
                input = new Input(new FileInputStream(path));
                return kryo.readObject(input,t);
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                close(input);
            }
        }
        return null;
    }

二、内存快照

写入:

 public static void takeSnapshot(String cachePath, String cacheFileName, PageCacheVO space) {
        ObjectOutputStream oos = null;
        try {
            File dir = new File(cachePath);

            if (!dir.exists()) {
                boolean mkdir = dir.mkdirs();
                if(!mkdir){
                    throw new RuntimeException("Failed to create directory!");
                }
            }
            File file = new File(cachePath,cacheFileName);
            FileOutputStream fos = new FileOutputStream(file);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(space);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

读取:


    public static PageCacheVO readSnapshot(String cachePath)  {
        try {
            File file = new File(cachePath);
            long startTime = System.currentTimeMillis();
            FileInputStream fis = new FileInputStream(file);
            startTime = System.currentTimeMillis();
            ObjectInputStream ois = new ObjectInputStream(fis);
            startTime = System.currentTimeMillis();
            PageCacheVO space = (PageCacheVO) ois.readObject();
            ois.close();
            return space;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

三、内存快照 + GZIP

写入:

public static void takeSnapshotGzip(String cachePath, String cacheFileName, PageCacheVO space) {
        ObjectOutputStream oos = null;
        try {
            File dir = new File(cachePath);

            if (!dir.exists()) {
                boolean mkdir = dir.mkdirs();
                if(!mkdir){
                    throw new RuntimeException("Failed to create directory!");
                }
            }
            File file = new File(cachePath,cacheFileName);
//            FileOutputStream fos = new FileOutputStream(file);

            FileOutputStream fos = new FileOutputStream(file);
            GZIPOutputStream gz = new GZIPOutputStream(fos);

            oos = new ObjectOutputStream(gz);
            oos.writeObject(space);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(oos!=null){
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

读取:

public static PageCacheVO readSnapshotGzip(String cachePath)  {
        try {
            File file = new File(cachePath);
            long startTime = System.currentTimeMillis();
            FileInputStream fis = new FileInputStream(file);
            startTime = System.currentTimeMillis();
//            ObjectInputStream ois = new ObjectInputStream(fis);
            GZIPInputStream gis = new GZIPInputStream(fis);
            ObjectInputStream ois = new ObjectInputStream(gis);
            startTime = System.currentTimeMillis();
            PageCacheVO space = (PageCacheVO) ois.readObject();
            ois.close();
            return space;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

执行测试的代码:

 public static void main(String[] args) {
        String name="张三";
        PageCacheVO pageCacheVO = new PageCacheVO();
        pageCacheVO.setBatchNumber(9527);
        Long startTime = System.currentTimeMillis();
        KryoUtil.serializeFile(pageCacheVO, "F:\\test\\kryo.cache");
        System.out.println("Kryo-存储序列化文件耗时:"+(System.currentTimeMillis()-startTime) );
        PageCacheVO reader1 = KryoUtil.unSerializeFile("F:\\test\\kryo.cache", PageCacheVO.class);
        startTime = System.currentTimeMillis();
        System.out.println("Kryo-读取序列化文件耗时:"+(System.currentTimeMillis()-startTime)+ "  解析出文件内容:"+reader1.getBatchNumber());

        startTime = System.currentTimeMillis();
        KryoUtil.takeSnapshot("F:\\test", "kryo2.cache",pageCacheVO);
        System.out.println("内存快照-存储序列化文件耗时:"+(System.currentTimeMillis()-startTime));

        startTime = System.currentTimeMillis();
        PageCacheVO reader2 = KryoUtil.readSnapshot("F:\\test\\kryo2.cache");
        System.out.println("内存快照-读取序列化文件耗时:"+(System.currentTimeMillis()-startTime)+ "  解析出文件内容:"+reader2.getBatchNumber());

        startTime = System.currentTimeMillis();
        KryoUtil.takeSnapshotGzip("F:\\test", "kryo3.cache",pageCacheVO);
        System.out.println("GZIP内存快照-存储序列化文件耗时:"+(System.currentTimeMillis()-startTime));

        startTime = System.currentTimeMillis();
        PageCacheVO reader3 = KryoUtil.readSnapshotGzip("F:\\test\\kryo3.cache");
        System.out.println("GZIP内存快照-读取序列化文件耗时:"+(System.currentTimeMillis()-startTime)+ "  解析出文件内容:"+reader3.getBatchNumber());

    }

执行结果: 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫竹修

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值