简体   繁体   English

LZ4与放气压缩管柱相比速度不快

[英]LZ4 is not fast compared to deflater compressing string

I tried to use LZ4 compression to compress a string object.But the result are not in favour to LZ4 Here is the program i tried 我尝试使用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);
    }
}

在此处输入图片说明 I have collected statics as above.But LZ4 is not that fast as stated Please let me where am i doing wrong. 我已经按照上面的方法收集了statics。但是LZ4的速度不如所陈述的快。请让我在哪里做错了。

Your results are meaningless because the size before compression is too small. 您的结果没有意义,因为压缩前的大小太小。 You are trying to measure the compression of a few thousand bytes at speeds over 100MB/s. 您正在尝试以超过100MB / s的速度测量几千个字节的压缩率。 The measurement is lost in the time taken by the JVM to warm up. 在JVM进行预热的时间内,测量值丢失了。 Try again with an input file of several MBs. 使用数MB的输入文件再试一次。 You should get numbers in line with my LZ4 implementation here: https://github.com/flanglet/kanzi/wiki/Compression-examples . 您应该在这里获得与我的LZ4实现相符的数字: https : //github.com/flanglet/kanzi/wiki/Compression-examples

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM