zoukankan      html  css  js  c++  java
  • Zstd-数据压缩组件

    Zstandard 简称Zstd,是一款快速实时的开源数据压缩程序,由Facebook开发,源码是用C语言编写的。相比业内其他压缩算法(如Gzip、Snappy、Zlib)它的特点是:当需要时,它可以将压缩速度交换为更高的压缩比率(压缩速度与压缩比率的权衡可以通过小增量来配置),反之亦

    Zstd-jni

    Zstd拥有丰富的API,几乎支持所有流行的编程语言,Zstd-jni 是Java中提供的API然。 它具有小数据压缩的特殊模式,称为字典压缩,可以从任何提供的样本集中构建字典。

    <dependency>

        <groupId>com.github.luben</groupId>

        <artifactId>zstd-jni</artifactId>

        <version>VERSION</version>

    </dependency>

    1.  
      /*
    2.  
      序列化
    3.  
      */
    4.  
      public static <T> byte[] serialize(T result) throws IOException {
    5.  
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(512);
    6.  
      try (OutputStream outputStream = new ZstdOutputStream(byteArrayOutputStream)) {
    7.  
      // protostuff serialize
    8.  
      ProtostuffSerializer.serialize(result, outputStream);
    9.  
      return byteArrayOutputStream.toByteArray();
    10.  
      }
    11.  
      }
    12.  
       
    13.  
      /*
    14.  
      反序列化
    15.  
      */
    16.  
      public static <T> T deserialize(byte[] bytes, Class<T> clazz) throws IOException {
    17.  
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    18.  
      try (InputStream inputStream = new ZstdInputStream(byteArrayInputStream)) {
    19.  
      // protostuff deserialize
    20.  
      return ProtostuffSerializer.deserialize(clazz, inputStream);
    21.  
      }
    22.  
      }
    1.  
      ZstdDictTrainer zstdDictTrainer = new ZstdDictTrainer(1024 * 1024, 32 * 1024);
    2.  
      // fileInput is a sample file
    3.  
      zstdDictTrainer.addSample(fileInput);
    4.  
      byte[] dic = zstdDictTrainer.trainSamples(true);
    5.  
       
    6.  
      /*
    7.  
      Zstd's training model
    8.  
      */
    9.  
      public static <T> byte[] serialize(T result) throws IOException {
    10.  
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(512);
    11.  
      try (ZstdOutputStream outputStream = new ZstdOutputStream(byteArrayOutputStream)) {
    12.  
      if (dic != null) {
    13.  
      outputStream.setDict(dic);
    14.  
      }
    15.  
      // protostuff serialize
    16.  
      ProtostuffSerializer.serialize(result, outputStream);
    17.  
      return byteArrayOutputStream.toByteArray();
    18.  
      }
    19.  
      }
    20.  
       
    21.  
      /*
    22.  
      Zstd's training model
    23.  
      */
    24.  
      public static <T> T deserialize(byte[] bytes, Class<T> clazz) throws IOException {
    25.  
      ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
    26.  
      try (ZstdInputStream inputStream = new ZstdInputStream(byteArrayInputStream)) {
    27.  
      if (dic != null) {
    28.  
      inputStream.setDict(dic);
    29.  
      }
    30.  
      // protostuff deserialize
    31.  
      return ProtostuffSerializer.deserialize(clazz, inputStream);
    32.  
      }
    33.  
      }

    服务器:VM/4CPU i5-6500/CentOS 6

    exp1: 4KB

    version

    compress

    decompress

    Ratio

    Gzip jdk1.8 0.162ms 0.059ms 2.31
    Snappy 1.1.7.2 0.005ms 0.002ms 1.83
    Zstd 1.3.7-3 0.033ms 0.012ms 2.27
    Zstd_Dic 1.3.7-3 0.020ms 0.029ms 3.81

    exp2: 16KB

    version

    compress

    decompress

    Ratio

    Gzip jdk1.8 0.279ms 0.126ms 4.29
    Snappy 1.1.7.2 0.022ms 0.007ms 3.18
    Zstd 1.3.7-3 0.049ms 0.002ms 4.39
    Zstd_Dic 1.3.7-3 0.062ms 0.017ms 5.70

    exp3: 43KB

    version

    compress

    decompress

    Ratio

    Gzip jdk1.8 0.767ms 0.339ms 6.49
    Snappy 1.1.7.2 0.111ms 0.044ms 4.36
    Zstd 1.3.7-3 0.257ms 0.018ms 6.67
    Zstd_Dic 1.3.7-3 0.199ms 0.061ms 8.12

    exp4: 134KB

    version

    compress

    decompress

    Ratio

    Gzip jdk1.8 1.786ms 1.026ms 13.34
    Snappy 1.1.7.2 0.894ms 0.595ms 6.53
    Zstd 1.3.7-3 0.411ms 0.198ms 14.74
    Zstd_Dic 1.3.7-3 0.220ms 0.089ms 16.48

    exp5:  654KB

    version

    compress

    decompress

    Ratio

    Gzip jdk1.8 4.587ms 1.865ms 33.64
    Snappy 1.1.7.2 2.069ms 1.430ms 8.86
    Zstd 1.3.7-3 2.864ms 0.116ms 45.57
    Zstd_Dic 1.3.7-3 0.426ms 0.218ms 47.38

    引用链接:

    https://github.com/facebook/zstd

    https://github.com/luben/zstd-jni

  • 相关阅读:
    C#多线程学习(四) 多线程的自动管理(线程池)
    CHR6dm 校准软件下载
    WinAPI打开串口失败
    AHRS(航姿参考系统)和IMU(惯性测量单元)的区别【转】
    课程1:开始Direct3D
    CHR6dm datasheet 中文翻译
    从D3D将画面渲染至桌面
    Hello GPU
    linux配置java环境变量(详细)
    java中的多线程
  • 原文地址:https://www.cnblogs.com/exmyth/p/14240764.html
Copyright © 2011-2022 走看看