iEntry 10th Anniversary RSS Newsletter Advertising
Join the WebProWorld Forum!
Text: Decrease Font Size Increase Font Size | Print Print Article | Share: Delicious Digg StumbleUpon Post to Twitter Post to Facebook
Friday, December 8, 2006

GZip vs. Deflate - Compression and Performance

6 Comments

Poor Test

What a rubbish test, please do a more in depth test!

Internally the same

GZip is just a wrapped set of headers around deflate.

Deflate includes a few small headers such as any initial data that will get better compression (almost always ignored), the compression rate (none/normal/best), etc. Deflate also includes an Adler32 checksum at the end.

GZip adds some additional headers to the start (algorithm [always deflate anyway], file name, unix style "chmod" permissions, etc) and a CRC32 checksum to the end.

This means that deflate will always be slightly faster (less to initialise, no CRC32 checksum) and save you a few bytes (no GZip headers or footers, just the deflate headers that are inside the GZip headers for GZip compression anyway).

However, the difference in speed will probably be negligible and I have not yet seen anything that tests the speed reliably.

It does not make sense to compare compression ratios as they both compress in exactly the same way (with the deflate algorithm). The only difference in size is the GZip headers/footers, which is only a few bytes anyway.

The same goes for decompression.

MORE IMPORTANTLY:
The built in .net implementation for deflate and gzip is not good. In my tests, it has been shown to be significantly slower and give significantly worse compression than the zlib implementation found in sharpziplib.

So, here's what to do:
- Make sure you check the Accept-Encoding header exists (is not null).
- Check for deflate and, if it's there, use that.
- Now that you know deflate isn't there, check for gzip and, if it's there, use that.
- Try using the sharpziplib implementation for better speed and performance.
- Check that whatever you are doing cleans up after itself!

what about compression ratio?

Mads,

While compression algorithm performance on the server is certainly an issue, the primary intent for HTTP compression is the reduction in data size for network transmission.  I'm wondering what the difference in compression ratio is for the two algorithms, given various types of data.

I'll be looking into this, too...

Dave

 

You are instantiating

You are instantiating objects inside the performance test, making it invalid.

Also you haven't tested how

Also you haven't tested how the decompression performs.

i tested this

i tested this myself


public static void Test()
{
int len =1024*1024;
byte[] text = new byte[len];
for (int i = 0; i < len; i++)
text[i] = (byte)'x';

Stopwatch sw=null;
byte[] compressed = null;
byte[] original = null;
long time = 0;

//GzipCompress(text);

DeflateCompress(text);

Console.ReadKey();
}

private static void GzipCompress(byte[] text)
{
Stopwatch sw;
long time;
sw = Stopwatch.StartNew();
byte[] compressed = CompressTool.GZipCompress(text);
byte[] original = CompressTool.GZipDecompress(compressed);
time = sw.ElapsedMilliseconds;
//Console.WriteLine(compressed.ToString());
Console.WriteLine(string.Format("gzip\noriginal size:{0}\ncompressed size:{1}\nCompression ratio: {2}%\nTime taken:{3}ms\n\n", original.Length, compressed.Length, (float)compressed.Length / original.Length * 100, time));
}

private static void DeflateCompress(byte[] text)
{
Stopwatch sw;
long time;
sw = Stopwatch.StartNew();
byte[] compressed = CompressTool.DeflateCompress(text);
byte[] original = CompressTool.DeflateDecompress(compressed);
time = sw.ElapsedMilliseconds;
//Console.WriteLine(compressed.ToString());
Console.WriteLine(string.Format("deflate\noriginal size:{0}\ncompressed size:{1}\nCompression ratio: {2}%\nTime taken:{3}ms", original.Length, compressed.Length, (float)compressed.Length / original.Length * 100, time));
}

for an array that is 1mb in size as you can plainly see:
gzip: 40-41ms
deflate: 27ms
the gzip form is a bit bigger.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1 + 3 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
SEARCH
Popular WPN Business Resources












Subscribe to WebProNews


Send me relevant info