`
cywhoyi
  • 浏览: 426561 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

File Serialization的加速度

阅读更多

File读写是我们经常对于File处理经常需要动作,接下来我会用4中方式进行操作,具体在项目中采用哪一种方式,由自己进行判断。

最近在Thomas Nagel《What Does It All Mean》,摘录开头一段话:

要评估每一种主张,每一条论证和每一套理论,并且尝试着判断它们是否可以被接受,这最终都依赖于每一个人的独立思考,而非听命于权威

2B青年方式:

  private static class StandardSerialization implements SerializationTest {
    public void testWriteBuffered(TestObject test, String fileName) throws IOException {
      ObjectOutputStream objectOutputStream = null;
      try {
        FileOutputStream fileOutputStream = new FileOutputStream(fileName);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        objectOutputStream = new ObjectOutputStream(bufferedOutputStream);
        objectOutputStream.writeObject(test);
      } finally {
        if (objectOutputStream != null) {
          objectOutputStream.close();
        }
      }
    }

    public TestObject testReadBuffered(String fileName) throws IOException, ClassNotFoundException {
      ObjectInputStream objectInputStream = null;
      try {
        FileInputStream fileInputStream = new FileInputStream(fileName);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        objectInputStream = new ObjectInputStream(bufferedInputStream);
        return (TestObject) objectInputStream.readObject();
      } finally {
        if (objectInputStream != null) {
          objectInputStream.close();
        }
      }
    }
  }

 普通青年:

private static class StandardSerializationRaf implements SerializationTest {
    public void testWriteBuffered(TestObject test, String fileName) throws IOException {
      ObjectOutputStream objectOutputStream = null;
      try {
        RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, FILE_MODE_RW);
        FileOutputStream fileOutputStream = new FileOutputStream(randomAccessFile.getFD());
        objectOutputStream = new ObjectOutputStream(fileOutputStream);
        objectOutputStream.writeObject(test);
      } finally {
        if (objectOutputStream != null) {
          objectOutputStream.close();
        }
      }
    }

    public TestObject testReadBuffered(String fileName) throws IOException, ClassNotFoundException {
      ObjectInputStream objectInputStream = null;
      try {
        RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, FILE_MODE_R);
        FileInputStream fileInputStream = new FileInputStream(randomAccessFile.getFD());
        objectInputStream = new ObjectInputStream(fileInputStream);
        return (TestObject) objectInputStream.readObject();
      } finally {
        if (objectInputStream != null) {
          objectInputStream.close();
        }
      }
    }
  }

 文艺青年:

采用Kryo Framework

  private static class Kryo2Serialization implements SerializationTest {

    private static Kryo kryo = new Kryo();

    public void testWriteBuffered(TestObject test, String fileName) throws IOException {
      Output output = null;
      try {
        RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
        output = new Output(new FileOutputStream(raf.getFD()), MAX_BUFFER_SIZE);
        kryo.writeObject(output, test);
      } finally {
        if (output != null) {
          output.close();
        }
      }
    }

    public TestObject testReadBuffered(String fileName) throws IOException {
      Input input = null;
      try {
        RandomAccessFile raf = new RandomAccessFile(fileName, "r");
        input = new Input(new FileInputStream(raf.getFD()), MAX_BUFFER_SIZE);
        return kryo.readObject(input, TestObject.class);
      } finally {
        if (input != null) {
          input.close();
        }
      }

    }
  }

 Geeker:

  private static class DirectSerialization implements SerializationTest {
    public void testWriteBuffered(TestObject test, String fileName) throws IOException {
      RandomAccessFile raf = null;
      try {
        MemoryBuffer memoryBuffer = new MemoryBuffer(MAX_BUFFER_SIZE);
        raf = new RandomAccessFile(fileName, FILE_MODE_RW);
        test.write(memoryBuffer);
        raf.write(memoryBuffer.getBuffer());
      } finally {
        if (raf != null) {
          raf.close();
        }
      }
    }

    public TestObject testReadBuffered(String fileName) throws IOException {
      RandomAccessFile raf = null;
      try {
        raf = new RandomAccessFile(fileName, FILE_MODE_R);
        MemoryBuffer unsafeBuffer = new MemoryBuffer((int) raf.length());
        raf.read(unsafeBuffer.getBuffer());
        return TestObject.read(unsafeBuffer);
      } finally {
        if (raf != null) {
          raf.close();
        }
      }
    }
  }

 最终输出的结果是:



 结论:

RandomAccessFile确实提高很明显

Kryo-dynamic serialization在write的时候优势明显,读取相差无几

UNSAFE_MEMORY确实高效,不过容易Memory Leak

  • 大小: 68.9 KB
分享到:
评论
1 楼 li15038043160_ 2013-11-04  
Java的序列化现在有些过时了,用json保存数据简洁高效无污染!

相关推荐

    C# 对象串行化输入输出 演示代码

    优点是速度快,数据紧凑,但缺点是不便于人类阅读,且与平台相关。 2. SoapFormatter:用于创建SOAP消息的串行化形式,适用于Web服务。 3. XmlSerializer:将对象转换为XML格式,易于理解和调试,但可能比二进制格式...

    JAVA文件传输器V2.0

    在进行文件传输时,可能会用到对象序列化(Serialization)技术,将对象转换为字节流以便在网络上传输。 拖拽功能的实现,依赖于JAVA的Swing或JavaFX图形用户界面库。在Swing中,可以使用DragAndDrop事件监听来捕获...

    网上聊天室 C# vs2008

    4. **多线程处理**:为了确保聊天室的响应速度和性能,多线程技术常被用于处理并发连接和接收消息。C#的System.Threading命名空间提供了丰富的多线程工具。 5. **数据序列化与反序列化**:为了让消息在客户端和...

    Redis数据库 v7.2.4.zip

    5. 协议支持:Redis使用简单明了的RESP(REdis Serialization Protocol)协议,易于与其他语言的客户端库集成。 二、Redis 7.2.4新特性: 1. 性能优化:每个新版本都会针对特定场景进行性能优化,7.2.4也不例外,...

    Java Platform Migration Guide.pdf

    为了提高渲染速度,Java 5.0引入了硬件加速的支持。 **1.2.4 X11 相关改进(X11-related Improvements)** 针对X11平台进行了一系列优化,特别是在Solaris和Linux系统上。 ##### 支持增补Unicode字符(Support for ...

    Python学习笔记。。。

    - 使用`.pyc`文件提高加载速度。 - **1.6 执行** - Python解释器如何执行字节码。 ##### 第2章:内置类型 - **2.1 数字** - 整数(int)、浮点数(float)和复数(complex)的使用。 - 算术运算符的应用。 - **2.2 ...

Global site tag (gtag.js) - Google Analytics