Compression benchmarking

By Sockreg on Dec 05, 2009

Illustrates the size benefits you can achieve by compressing a stream. It also benchmarks the time taken to write the compressed text and the normal text to the underlying files.

/*
Author: Sockreg
Occupation: Student, .NET Enthusiast
Usage: Start a console application and paste the following code. Files are created in C:\ and are named Zipped.zip and NotZipped.txt
Functionality: To illustrate the size benefits you can achieve by compressing a stream. It also benchmarks the time taken to write the compressed text
               and the normal text.
*/

using System;
using System.IO.Compression;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;

class MainClass {

    static void Main() {
        Nullable<long> timeForZip = null;
        Nullable<long> timeForNormalText = null;

        Stopwatch zipEllapse = new Stopwatch();
        zipEllapse.Start();
        FileStream myFileStream = new FileStream(@"C:\Zipped.zip", FileMode.OpenOrCreate);
        using (GZipStream myZippedStream = new GZipStream(myFileStream,
                                                    CompressionMode.Compress)) {
            StringBuilder textToZip = new StringBuilder();
            for (int count = 0; count <= 200000; count++) { textToZip.Append(String.Format("MyText{0}  \n", count)); }
            var byteArray = Encoding.Default.GetBytes(textToZip.ToString());
            myZippedStream.Write(byteArray, 0, byteArray.Length);
            myZippedStream.Flush(); zipEllapse.Stop(); timeForZip = 
                                        zipEllapse.ElapsedMilliseconds;
            using (FileStream mySecondFileStream = new FileStream(@"C:\NotZipped.txt",
                                                                        FileMode.OpenOrCreate)) {
                Stopwatch normalEllapse = new Stopwatch();
                normalEllapse.Start();
                StringBuilder textToWrite = new StringBuilder();
                for (int count = 0; count <= 200000; count++) { textToWrite.Append(String.Format("MyText{0}  \n", count)); }
                var normalTextByteArray = Encoding.Default.GetBytes(textToWrite.ToString());
                mySecondFileStream.Write(normalTextByteArray, 0, normalTextByteArray.Length);
                mySecondFileStream.Flush();
                normalEllapse.Stop(); timeForNormalText = normalEllapse.ElapsedMilliseconds;
            }
        }

        FileInfo myFile = new FileInfo(@"C:\Zipped.zip"); 
        Console.WriteLine(String.Format(@"Zipped file: {0} bytes = {1} mbs {3}Time taken: {2} Milliseconds{3}{3}", 
                                    myFile.Length, Math.Round(((decimal)myFile.Length/1024/1024), 4), 
                                    timeForZip, Environment.NewLine));

        myFile = new FileInfo(@"C:\NotZipped.txt");
        Console.WriteLine(String.Format(@"Not Zipped file: {0} bytes = {1} mbs {3}Time taken: {2} Milliseconds",
                                    myFile.Length, Math.Round(((decimal)myFile.Length / 1024 / 1024), 4),
                                    timeForNormalText, Environment.NewLine));

        Console.ReadLine();
    }
}

Comments

Sign in to comment.
^Neptune   -  Dec 05, 2009

I didn't even know you could compress streams. Neat.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.