0

我尝试使用 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);
    }
}

在此处输入图像描述 我已经收集了上面的静态数据。但是 LZ4 并没有说的那么快请让我在哪里做错了。

4

1 回答 1

3

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

于 2016-04-18T06:29:38.770 回答