Quantcast

GZip vs. Deflate – Compression and Performance

Get the WebProNews Newsletter:


[ Business]

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/

GZip vs. Deflate – Compression and Performance
About Mads Kristensen
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/ WebProNews Writer
Top Rated White Papers and Resources
  • Guest

    Also you haven’t tested how the decompression performs.

    • Guest

      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.

  • Guest

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

  • http://www.dawkco.com/ Guest

    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

     

  • George

    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!

  • Guest

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

  • Guest

    Wow this review is crap

  • Guest

    Your test is faulty, the buffer used in the test is incorrectly initialized (containing only zero’s).
    A test with binary and text files shows that there is a difference however nowhere near 40%.
    DeflateStream outperforms GZipStream by about 8 percent in time (the compression rate are almost identical)

  • Manpreet Singh

    Yes Deflate rely works very fast than GZip…

    Thanks for Knowledge Transfer

    -Manpreet Singh

  • http://freeiphones.cz.cc/vbulletin/member.php?u=89025 hp mini 210

    I would just like to let you know hvow much I learn from reading articles Dugg you.Hope 2 be back fast for some more goodies

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom