java - 与 deflater 压缩字符串相比,LZ4 并不快

标签 java compression lz4

我尝试使用LZ4压缩来压缩字符串对象,但结果不利于LZ4 这是我尝试过的程序

public class CompressionDemo {

    public static byte[] compressGZIP(String data) throws IOException {
        long start = System.nanoTime ();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length());
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(data.getBytes());
        gzip.close();
        byte[] compressed = bos.toByteArray();
        bos.close();
        System.out.println(System.nanoTime()-start);
        return compressed;
    }

    public static byte[] compressLZ4(String data) throws IOException {
        long start = System.nanoTime ();
        LZ4Factory factory = LZ4Factory.fastestJavaInstance();
        LZ4Compressor compressor = factory.highCompressor();
        byte[] result = compressor.compress(data.getBytes());
        System.out.println(System.nanoTime()-start);
        return result;

    }

    public static byte[] compressDeflater(String stringToCompress) {
        long start = System.nanoTime ();
        byte[] returnValues = null;
        try {
            Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
            deflater.setInput(stringToCompress.getBytes("UTF-8"));
            deflater.finish();
            byte[] bytesCompressed = new byte[Short.MAX_VALUE];
            int numberOfBytesAfterCompression = deflater.deflate(bytesCompressed);
            returnValues = new byte[numberOfBytesAfterCompression];
            System.arraycopy(bytesCompressed, 0, returnValues, 0, numberOfBytesAfterCompression);
        } catch (Exception uee) {
            uee.printStackTrace();
        }
        System.out.println(System.nanoTime()-start);
        return returnValues;
    }



    public static void main(String[] args) throws IOException, DataFormatException {
        System.out
                .println("..it’s usually most beneficial to compress anyway, and determine which payload (the compressed or the uncompressed one) has the smallest size and include a small token to indicate whether decompression is required."
                        .getBytes().length);
        byte[] arr = compressLZ4("..it’s usually most beneficial to compress anyway, and determine which payload (the compressed or the uncompressed one) has the smallest size and include a small token to indicate whether decompression is required.");
        System.out.println(arr.length);
    }
}

enter image description here 我已经收集了上面的统计数据。但是LZ4并没有所说的那么快 请告诉我我哪里做错了。

最佳答案

你的结果没有意义,因为压缩前的大小太小。您正在尝试以超过 100MB/s 的速度测量几千字节的压缩情况。测量值会在 JVM 预热期间丢失。使用几 MB 的输入文件重试。您应该在此处获得与我的 LZ4 实现一致的数字:https://github.com/flanglet/kanzi/wiki/Compression-examples .

关于java - 与 deflater 压缩字符串相比,LZ4 并不快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36352159/

相关文章:

java - VoltDB createConnection 超时

java - 为 Weka 生成 Arff 文件

java - spring ibatis mysql间歇性异步问题

java - 如何访问 CompletableFuture 链中所有先前的 CompletionStage 结果

python - Pandas to_hdf 溢出错误

java - 桌面应用程序应该压缩 jar 文件吗?

linux - 自定义解压缩函数BASH

c - 无损压缩算法是否在位级别上工作?

使用输入/输出流的 Java LZ4 压缩