栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

一、Kryo

  写入文件:

public static   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 unSerializeFile(String path,Class 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());

    }
执行结果: 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921993.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号