private static void Test(string directory, ILZ4Compressor compressor, ILZ4Decompressor decompressor) { var w = new Stopwatch(); var dw = new Stopwatch(); long compressedTotal = 0; long uncompressedTotal = 0; for(int j=0;j<10;j++) foreach (var bytes in Read(directory)) { uncompressedTotal += bytes.Length; byte[] compressed = new byte[compressor.CalculateMaxCompressedLength(bytes.Length)]; w.Start(); int compressedLength = compressor.Compress(bytes, compressed); compressedTotal += compressedLength; w.Stop(); byte[] uncompressed = new byte[bytes.Length]; dw.Start(); decompressor.DecompressKnownSize(compressed, uncompressed, uncompressed.Length); dw.Stop(); for (int i = 0; i < uncompressed.Length; i++) { if (uncompressed[i] != bytes[i]) throw new Exception("Original bytes and decompressed bytes differ starting at byte " + i); } } Console.WriteLine("Ratio = " + compressedTotal * 1.0 / uncompressedTotal); Console.WriteLine("Compression Time (MB / sec) = " + uncompressedTotal / 1024.0 / 1024.0 / w.Elapsed.TotalSeconds); Console.WriteLine("Uncompression Time (MB / sec) = " + uncompressedTotal / 1024.0 / 1024.0 / dw.Elapsed.TotalSeconds); }
/// <summary> /// Compresses the data /// </summary> /// <param name="data">The data to compress</param> /// <param name="offset">The position where the data begins</param> /// <param name="length">The length to compress</param> /// <returns></returns> public override byte[] Encode(ref byte[] data, ref uint offset, ref uint length, ref PacketHeader packetHeader) { byte[] compressed = new byte[Lz4Compressor.CalculateMaxCompressedLength((int)length)]; int CompressedSize = Lz4Compressor.Compress(data, (int)offset, (int)length, compressed, 0); if (CompressedSize < length) { offset = 0; length = (uint)CompressedSize; return(compressed); } return(data); }
private static void Test(string directory, ILZ4Compressor compressor, ILZ4Decompressor decompressor) { var w = new Stopwatch(); var dw = new Stopwatch(); long compressedTotal = 0; long uncompressedTotal = 0; for (int j = 0; j < 10; j++) { foreach (var bytes in Read(directory)) { uncompressedTotal += bytes.Length; byte[] compressed = new byte[compressor.CalculateMaxCompressedLength(bytes.Length)]; w.Start(); int compressedLength = compressor.Compress(bytes, compressed); compressedTotal += compressedLength; w.Stop(); byte[] uncompressed = new byte[bytes.Length]; dw.Start(); decompressor.DecompressKnownSize(compressed, uncompressed, uncompressed.Length); dw.Stop(); for (int i = 0; i < uncompressed.Length; i++) { if (uncompressed[i] != bytes[i]) { throw new Exception("Original bytes and decompressed bytes differ starting at byte " + i); } } } } Console.WriteLine("Ratio = " + compressedTotal * 1.0 / uncompressedTotal); Console.WriteLine("Compression Time (MB / sec) = " + uncompressedTotal / 1024.0 / 1024.0 / w.Elapsed.TotalSeconds); Console.WriteLine("Uncompression Time (MB / sec) = " + uncompressedTotal / 1024.0 / 1024.0 / dw.Elapsed.TotalSeconds); }