Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)
Log In
UserName Or Email:

Password:

Auto-Login:

Miscellaneous Links
  Submit Article
Stardeveloper RSS Feed
Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Gzip vs Deflate: Which is the faster HTTP compression method?
 
RSS - Read full length articles at Stardeveloper using Stardeveloper RSS Feed RSS

Gzip vs Deflate: Which is the faster HTTP compression method?

by Faisal Khan. Follow Faisal Khan on Twitter Follow Faisal Khan on Facebook

In this article we will learn about Gzip and Deflate, which are two popular HTTP compression methods. In a previous article, Enabling Gzip and Deflate HTTP Compression in ASP.NET pages, I talked about how to enable HTTP compression of the text/html output in ASP.NET pages using ASP.NET events in Global.asax file. While talking about Gzip and Deflate in that article, a question arose in my mind (as would have risen in the minds of so many ASP.NET developers and webmasters) that which of these methods is faster and how do they compare with each other in raw speed? If we can get an answer to this question, we can by default enable the HTTP compression method which is faster and let the slower one only be used if the browser doesn't support the faster HTTP compression method. This will not only make our web pages load faster, but will also take less CPU time on our server.

Read the rest of this article to find out: What is HTTP compression? What are Gzip and Deflate compression algorithms? What code did I write to compare the speed of these two HTTP compression methods? Which one came out to be faster and by how much? And which HTTP compression method, as a result of the tests that I ran, I have enabled by default for ASP.NET pages at Stardeveloper.com?

What is HTTP Compression?
HTTP stands for "Hyper Text Transfer Protocol". This protocol allows text and binary data to be sent between client browser and web server. Text data is mostly sent in an uncompressed format. HTTP compression allows this text data to be compressed using one of the two HTTP compression methods that we will discuss later. Compressed data takes much less space and saves important bandwidth, especially for users accessing the web page over dial-up connections. HTTP compression can save as much as 70%-80% bandwidth and at the same time, for the end-user, the page loads much faster. For example, HTTP compression makes 41 KB home page of Stardeveloper.com appear as 8.5 KB to the user.

Imagine you visit a web site, example1.com, which loads in 4 seconds using HTTP compression, and a web site, example2.com, which loads in 16 seconds without using HTTP compression; assuming that the web page size of both web sites is the same. Which of the two are you most likely to visit again? Won't example1.com provide much quicker and smoother surfing experience compared to example2.com? This is one of the most important reasons why webmasters use HTTP compression on their webs sites.

What is Deflate Compression Algorithm?
It is an algorithm for lossless file compression and decompression. It uses a combination of LZ77 algorithm and Huffman coding. You can learn more about it here. For the purpose of this article, all you need to know is that it is the standard algorithm used to compress files and data.

What is Gzip Compression?
Gzip is file compression that is based on Deflate algorithm. In other words, Gzip internally compresses a file using Deflate algorithm and then adds some extra information on top of this compressed file. You can learn more about it here. For the purpose of this article, you just need to know that a Gzip compressed file is a Deflate compressed file with some added headers and information (Cyclic Redundancy Checks) on top of it.

To put it succinctly, a Gzip compressed file will always be a few bytes more in size than a Deflate compressed file. Now that we know this, we have to figure out which of these two compression methods is faster (and by how much) for our web pages.

How to Enable Gzip or Deflate HTTP Compression on your Website?
There is a good article here at Stardeveloper.com that I wrote about enabling HTTP compression: Enabling Gzip and Deflate HTTP Compression in ASP.NET pages.

Speed Test: Which HTTP Compression Method is Faster?
To figure out which HTTP compression method is faster, I created a small C# application. In this C# application, I wrote down code to compress and decompress an HTML file of approx. 23 KB in size. Please first have a look at the code below and after that I will explain what it does:

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;

namespace GzipVsDeflate
{
  class Program
  {
    const string TextFileLocation = @"D:\Stardeveloper_old\test.htm";
    const int BufferSize = 100;

    static byte[] ContentBuffer = null;
    static int gzipCount = 0;
    static int deflateCount = 0;

    static volatile bool keepRunning = true;

    static void Main(string[] args)
    {
      Console.WriteLine("Starting...");
      Console.WriteLine();

      ContentBuffer = File.ReadAllBytes(TextFileLocation);

      Thread worker1 = new Thread(new ParameterizedThreadStart(Compress));
      Thread worker2 = new Thread(new ParameterizedThreadStart(Compress));

      worker1.Name = "Gzip Thread";
      worker2.Name = "Deflate Thread";

      worker1.Start("Gzip");
      worker2.Start("Deflate");

      Console.ReadLine();

      keepRunning = false;
      worker1.Join();
      worker2.Join();

      double diff = Math.Round(((float)deflateCount / (float)gzipCount) * 100F, 0);
      diff -= 100D;

      Console.WriteLine();
      Console.WriteLine("Result: Deflate is {0}% faster than Gzip.", diff);

      Console.WriteLine();
      Console.WriteLine("Exiting...");
    }

    static void Compress(object data)
    {
      string compressionMethod = data as string;

      MemoryStream ms = new MemoryStream();
      byte[] buffer = new byte[ContentBuffer.Length + BufferSize];
      Stream s = null;
      int count = 0;

      while (keepRunning)
      {
        ms.Position = 0;

        // Compressing
        if (compressionMethod == "Gzip")
          s = new GZipStream(ms, CompressionMode.Compress, true);
        else if (compressionMethod == "Deflate")
          s = new DeflateStream(ms, CompressionMode.Compress, true);

        s.Write(ContentBuffer, 0, ContentBuffer.Length);
        s.Dispose();
        s = null;

        ms.Position = 0;

        // Decompressing
        if (compressionMethod == "Gzip")
          s = new GZipStream(ms, CompressionMode.Decompress, true);
        else if (compressionMethod == "Deflate")
          s = new DeflateStream(ms, CompressionMode.Decompress, true);

        int offset = 0, read = 0;

        do
        {
          read = s.Read(buffer, offset, BufferSize);

          offset += read;
        }
        while (read > 0);

        s.Dispose();
        s = null;

        count++;
      }

      // Exiting
      Console.WriteLine("{0} exiting with {1} count...",
        Thread.CurrentThread.Name, count);

      if (compressionMethod == "Gzip")
        gzipCount = count;
      else if (compressionMethod == "Deflate")
        deflateCount = count;
    }
  }
}

 ( 1 Remaining ) Next

Comments/Questions ( Threads: 1, Comments: 2 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. compression in http module.. ( 1 Reply ) This thread contains 1 reply by the Author of this Article. This thread contains 1 reply by Faisal Khan.

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.