Click here to Skip to main content
15,887,477 members
Articles / Programming Languages / C#
Tip/Trick

ZIP Benchmark

Rate me:
Please Sign up or sign in to vote.
3.91/5 (7 votes)
7 Apr 2016CPOL 14.3K   4   2
A simple zip benchmark

Introduction

this is not a Compression comparison, but it is to test your compression speed on your pc.

I created this program as a simple zip benchmark which can easily benchmark zips.

Background

This program uses ClosedXML (don't forget to include this in your project reference!) and DotNetZip.

This program is usefull,
a. if you want to test how much you can compress certain files

or

b if you want to test your computers compression speed.

Using the Code

This is the code for executing the benchmark:

C#
static void Main(string[] args)
{
    Console.WriteLine("Working...");
    StreamWriter sw = new StreamWriter("log.txt");
    XLWorkbook workbook = new XLWorkbook();
    DataTable table = new DataTable();
    table.Columns.Add("Name:", typeof(string));
    table.Columns.Add("Uncompressed:", typeof(string));
    table.Columns.Add("Compressed kb:", typeof(string));
    table.Columns.Add("Compressed b:", typeof(string));
    table.Columns.Add("Ratio:", typeof(string));
    table.Columns.Add("% Compressed:", typeof(string));
    table.Columns.Add("Kilobytes compress: ", typeof(double));
    foreach (string s in Files())
    {
        Console.WriteLine("Zipping files...");
        ZipFile zf = new ZipFile();
        zf.AddFile("Files\\" + s);
        zf.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
        string sName = "Zip\\" + s.Split('.')[0] + ".zip";
        zf.Save(sName);
        Console.WriteLine("Done with zipping" + s + ".");

    }
    Console.Clear();
    foreach (string s in Files())
    {
        Console.WriteLine("Adding results to log.txt/log.xlsx...");
        string sName = "Zip\\" + s.Split('.')[0] + ".zip";
        long UnComp = new FileInfo("Files\\" + s).Length / 1024;
        long Comp = new FileInfo(sName).Length / 1024;
        double UnCompB = (double)new FileInfo("Files\\" + s).Length;
        double CompB = (double)new FileInfo(sName).Length;
        sw.WriteLine(s + "= Uncompressed: " + (UnComp).ToString() + 
        "kb " + "Compressed: " + (Comp).ToString() + "kb / 
        " + CompB + "bytes " + " Ratio: " + 
        ((UnCompB / CompB) / 10) + "%");
        table.Rows.Add(s, (UnComp).ToString() + "kb", 
        (Comp).ToString() + "kb", CompB + "bytes", 
        ((UnCompB / CompB)), ((CompB / UnCompB)) * 100 + "%", 
        (double)((UnCompB - CompB) / 1024));
        Console.Clear();
    }
    Console.WriteLine("Working...");

    Console.WriteLine("Saving log....");

    workbook.Worksheets.Add(table, "Data");

    workbook.SaveAs("log.xlsx");
    sw.Close();
    Console.WriteLine("Done.");

    Console.WriteLine("Do you want too start log.xlsx? (y/n)");
    switch (Console.ReadLine().ToLower())
    {
        case "y":
            Process.Start("log.xlsx");
            break;
    }
    Console.Clear();
    Console.WriteLine("Do you want too start log.txt? (y/n)");
    switch (Console.ReadLine().ToLower())
    {
        case "y":
            Process.Start("log.txt");
            break;
    }
}

and this is the code for getting the files:

C#
public static string[] Files()
{
    List<string> Result = new List<string>();
    foreach (string file in Directory.GetFiles("Files\\"))
    {
        var f = new FileInfo(file);
        Result.Add(f.Name);
    }
    return Result.ToArray();
}

Put all the files you want to benchmark in the Files\\ folder.

ClosedXML (don't forget to include this in your project reference!)

Points of Interest

The code is not very complex, but I hope you will find it useful!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI don't get it Pin
FantasticFiasco5-Apr-16 1:07
FantasticFiasco5-Apr-16 1:07 
I thought it was a zip speed/performance benchmark by the title, but it is a compression comparison?

And no graphs at all? When should I use it? Why should I use it?
SuggestionMore explanation please ! Pin
RickZeeland3-Apr-16 5:02
mveRickZeeland3-Apr-16 5:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.