Gzipping files on the fly part 1.

SDK Version: 

Today we wanted to test ways, to optimize the download of files to android phones.
The first way we tried, is the most obvious method of using a little less of everything (bandwith, battery, cpu, backlight, time? etc), compression.

Image source.

But what about speed? Is it worth to sacrifice the app's speed for using less bandwith? How much slower is compressing/decompressing files on a phone anyway?
Read on to find out.

The site I was testing on is: http://www.desilva.biz/gzip-test.php, its size is 14 Kb. You can get good compression rates with large text files, which we would want to use in the future, but right now, our top priority would be using a lot of smaller files, with mixed content.
Here is a little test code that I used:

  1. //put this part in your onCreate
  2. //gzipped run
  3. Long timeStart=System.currentTimeMillis();
  4. htmlDownloader("http://www.desilva.biz/gzip-test.php",true);
  5. Long timeEnd=System.currentTimeMillis()-timeStart;
  6. Log.d("gzipped run time:",timeEnd.toString());
  8. //non gzipped run
  9. timeStart=System.currentTimeMillis();
  10. htmlDownloader("http://www.desilva.biz/gzip-test.php",false);
  11. timeEnd=System.currentTimeMillis()-timeStart;
  12. Log.d("non gzipped run time:",timeEnd.toString());

  1. public void htmlDownloader(String url, boolean allowGzip) throws ClientProtocolException, IOException {
  2.         HttpResponse response = null;
  3.         HttpUriRequest request = new HttpGet(url);
  5.         if(allowGzip){
  6.                 request.addHeader("Accept-Encoding", "gzip");
  7.         }
  8.         DefaultHttpClient httpClient = new DefaultHttpClient();
  9.         response = httpClient.execute(request);
  11.         InputStream instream = response.getEntity().getContent();
  12.         Header contentEncoding = response.getFirstHeader("Content-Encoding");
  13.         if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
  14.             instream = new GZIPInputStream(instream);
  15.             Log.d("gzip?", "yes!");
  17.         } else {
  18.                 Log.d("gzip?", "nay");
  19.         }
  20. }

Here are a few test measurements, these values are in milliseconds:

Emulator 1.5 on lan (12Mbit/1Mbit) cable net:
gzipped: 7949,5442,8256, 5824, 4768
non gzipped: 1064,1063,1406, 951, 1225

Emulator 1.5 on wifi(2Mbit,1Mbit) cable net:
gzipped: 3120, 1424, 1678, 1722, 3442
non gzipped: 1395, 1148, 927, 963, 1376

Nexus one 2.2 on 3g(2Mbit/1,2Mbit):
gzipped: 3365, 2000, 3319, 3053, 3138
non gzipped: 935, 1117, 983, 1045, 914

Nexus one 2.2 on wifi(12Mbit/1Mbit) cable net:
gzipped: 857,896,901,863,903
non gzipped: 733,750,703,705,732

As you can see, in terms of speed, gzipped content is always slower.
The Nexus one can beat a laptop with a dual core 2 GHz processor+3Gb ram in compression, which is just weird. The adsl measurements were done on a quad core 2,8Ghz amd with 4 Gb of ram. Those values are slightly better, but the nexus still wins. :)

Right now I can't say for sure, that compression is the way to go, because with just a few measurements, it seems that compression is 2-3x slower than non compressed. If your UI needs speed, than compression is a no-go.

These measurements were few, and are just the tip of the iceberg, until next week, I'll do some more testing, with different file sizes, and different api levels, cpu usage etc.
Any suggestions on the test method, would be appreciated.