iEntry 10th Anniversary RSS Newsletter Advertising
Join the WebProWorld Forum!

GZip vs. Deflate - Compression and Performance

Post to Twitter Post to Facebook

After I wrote about a HTTP compression module in ASP.NET 2.0 one of my colleagues pointed out that the Deflate compression is faster than GZip.

Because the HTTP compression module chooses GZip over Deflate if the browser allows it, I thought that I'd better make a quick performance test just to be sure. I used this little test method to give me the answer I was looking for:

using System.IO.Compression;
using System.IO;
using System.Diagnostics;

   private void PerformanceTest()
{
   byte[] buffer = new byte[5000];
   using (MemoryStream stream = new MemoryStream())
   {
     Stopwatch sw = new Stopwatch();
     sw.Start();
     for (int i = 0; i < 1000; i++)
     {
       GZipStream gzip = new GZipStream(stream, CompressionMode.Compress);
       gzip.Write(buffer, 0, buffer.Length);
     }

     sw.Stop();
     Response.Write(sw.ElapsedMilliseconds);
   }
}

First I tested the GZipStream and then the DeflateStream. I expected a minor difference because the two compression methods are different, but the result astonished me. I measured the DeflateStream to 41% faster than GZip. That's a very big difference. With this knowledge, I'll have to change the HTTP compression module to choose Deflate over GZip.

Comments

Add to Del.icio.us | Digg | Reddit | Furl

Bookmark WebProNews:

Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

About the author:
Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution.

http://www.madskristensen.dk/

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 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Featured Headline
Fake Chrome OS Screenshots Punk Tech Media
Mystery Blogger Comes Clean
5 comments | 17 hours ago
 
Subscribe to WebProNews


Send me relevant info