Click here to Skip to main content
12,759,875 members (31,385 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 23 Dec 2006

QuickLZ Pure C# Port

, 23 Dec 2006
Rate this:
Please Sign up or sign in to vote.
This article describes a C# port of the new QuickLZ compression algorithm


It seems there is no end to the porting of good C/C++ algorithms to .NET, and so here comes another one.  This algorithm comes courtesy of Lasse Mikkel Reinhold, and is called QuickLZ.

You can read more about QuickLZ at the website, however I will outline some of the aspects of QuickLZ as I go.

What IS QuickLZ?

It comes as no surprise if you haven't heard of QuickLZ, it is a new compression algorithm particularily well suited for psuedo streaming (packet compression).  It was originally released early November 2006, and updated to 1.10 later that month.  The porting is based on 1.10, which as of the writing of this article boasts an incredible 20-30% improvement over other well known algorithms such as LZO1X and LZF.  Complete benchmark information on the C implementation can be found at the above linked website, however here is an overall assessment:

QuickLZ 1.10LZO 1X 2.02LZP-1LZF 1.6ZLIB-1 1.2.3
Compressed Size61.0%60.2%59.9%60.7%46.4%
Compression MB/Sec14881.860.460.97.45
Decompression MB/Sec38030789.3198120

If you are reading this article, there is a chance you also read my article including a port of MiniLZO.  Based on the numbers here, it's easy to see why LZO was my previous choice.  However, QuickLZ is now 19.2% faster on average than LZO on decompression, and an astonishing 44.7% faster on compression.  And approximately 0.8% less efficient on final compression.  This large improvement by QuickLZ made it a prime candidate for porting to .NET and while it doesn't totally replace my minilzo port, it does give a better alternative for those who don't need the LZO format.

Things To Remember

While I was porting the code, which was incredibly clear and easy to port, I noticed a number of areas for improvement.  This specific iteration of the port is a first revision and is based on the optimizations for better ratio working with data > 100kb in size.  A more refined and "Minimalistic" version of the algorithm is possible, and may come as a complimentary article specifically regarding packet compression.  Meanwhile, this class serves as a general use class for both files and packets that should outperform other managed options.  I have not profiled this port to C# whatsoever, and I know that for simplicity of the public interface there is some additional overhead.  If you must make the Unsafe versions public and use them, make sure you understand the code before you cry to me about it not working :)

A comment made during the port of MiniLZO was how the original uncompressed size was not easy to attain from the compressed data, so prepending the size was requested but would ultimately break compatibility with the original LZO1X implementation.  Based on the standard implementation of QuickLZ, I have provided a public method to find the original uncompressed size quickly and painlessly.

Without Further Ado...

So let's look at how to use the safe(?) interface to the implementation.  All source examples are psuedo, and assume knowledge of basic C# operations.

byte[] data = File.ReadAllBytes("...");
byte[] compressed = QuickLZ.Compress(data, 0, (UInt32)data.Length);

This little snippet shows the basic idea of obtaining some data, in this case loading it from a file, and passing it into the compression method, which takes a Byte[] for the buffer to read from, a starting offset within the buffer to start at, and the length from the starting point to include in compression.  This is useful if you need to work with a packet, where packet headers may not be included in the compression/decompression routines.  The return value is an exact size Byte[] stripped of the extra scratch space allocated during compression.  It should be noted that QuickLZ imposes a requirement that during compression the destination buffer is required to be the length of the original data plus an additional 36000 bytes for temporary workspace.  This implementation copies the temporary destination buffer into an exact size buffer for final return.

UInt32 size = QuickLZ.GetDecompressedSize(data, 0);

This little snippet here shows how you can manually obtain the size of the compressed data out of the Byte[] directly.  The second argument, again, reflects where the actual compressed data headers begin.  It should be noted that this method is never really needed, but is provided for completion.

byte[] decompressed = QuickLZ.Decompress(compressed, 0);

This last snippet is fairly self explanitory by this point. It takes a Byte[] for the compressed data, and a starting point once again.  It returns an exact size buffer like Compress, however does not have the added overhead of copying from a temporary destination, as the exact uncompressed size is known ahead of time, which is why the GetDecompressedSize method is not required.


QuickLZ as a new alternative to the compression scene and seems to currently be the fastest within this category of compression.  I hope you will find this port useful, and as always if you find any bugs or bottlenecks that degrade performance please let me know.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Canada Canada
Short and simple, I'm a self contracted programmer, my strongest programming skills are in C/C++ and C#/.NET. I have a nack for porting C algorithms to C#.

You may also be interested in...

Comments and Discussions

QuestionC++ equivalent Pin
Member 421131020-Aug-16 5:22
memberMember 421131020-Aug-16 5:22 
GeneralMy vote of 5 Pin
Simon Raffl28-Jun-13 6:25
memberSimon Raffl28-Jun-13 6:25 
GeneralMy vote of 5 Pin
Michael Latham14-Oct-12 21:38
memberMichael Latham14-Oct-12 21:38 
GeneralWhat's the license for using this code? None is listed. Pin
Wayne Walter21-Nov-09 10:29
memberWayne Walter21-Nov-09 10:29 
GeneralRe: What's the license for using this code? None is listed. Pin
Astaelan21-Nov-09 12:18
memberAstaelan21-Nov-09 12:18 
GeneralRe: What's the license for using this code? None is listed. Pin
Wayne Walter23-Nov-09 6:20
memberWayne Walter23-Nov-09 6:20 
GeneralMuch better alternative... [modified] Pin
captainplanet012330-Aug-08 11:07
membercaptainplanet012330-Aug-08 11:07 

Just a couple of points:

1) The table in the beginning of the article, showing QuickLZ to be twice as fast than LZF is misleading. Dead | X|
My benchmarks of compressing/decompressing using QuickLZ and LZF prove the author wrong:
I'm using the C# versions (managed only) of these two algorithms.

Random 4.5MB word doc file
C# QuickLZ: done in 171 ms, compressed to ~ 2.86 MB
C# LZF : done in 203 ms, compressed to ~ 2.00 MB WTF | :WTF:

This is in contrast to the author's claims that QuickLZ is twice as fast that LZF and better at compressing!!!
To me it seems like LZF in its default C# port is almost identical in performance and much better in compression ratios. Cool | :cool:

NOTE: After some optimizations, I have made LZF do it in 156 ms, compressing to ~ 2.00 MB. Roll eyes | :rolleyes:
This means its faster than QuickLZ. Shucks | :-\

2) If you have licensing issues with the GPL, again, the LZF algorithm proves to be a better alternative:
By the way you can get it from here:

It is licensed under BSD and GPL, you decide, so you can use it in binary form without open sourcing your project if u choose BSD.

For reference, this is the test code for QuickLZ:

byte[] rawData = File.ReadAllBytes("c:\\test.txt");
            DateTime pre = DateTime.Now;
            byte[] outputData = QuickLZSharp.QuickLZ.compress(rawData);
            rawData = QuickLZSharp.QuickLZ.decompress(outputData);
            DateTime post = DateTime.Now;
            Console.WriteLine(post - pre);

and for LZF:

QuickLZF qlzf = new QuickLZF();
            byte[] rawData = File.ReadAllBytes("c:\\test.txt");
            DateTime pre = DateTime.Now;
            byte[] outputData = new byte[rawData.Length];
            int result = qlzf.lzf_compress(rawData, rawData.Length, outputData, rawData.Length);
            int result2 = qlzf.lzf_decompress(outputData, result, rawData, rawData.Length);
            DateTime post = DateTime.Now;
            Console.WriteLine(post - pre);

Regards D'Oh! | :doh:

modified on Saturday, August 30, 2008 7:22 PM

GeneralRe: Much better alternative... [modified] Pin
rlasse15-Sep-08 2:39
memberrlasse15-Sep-08 2:39 
Newsbetter alternative... still!!! Pin
captainplanet012318-Sep-08 13:06
membercaptainplanet012318-Sep-08 13:06 
GeneralRe: better alternative... still!!! [modified] Pin
rlasse18-Sep-08 14:22
memberrlasse18-Sep-08 14:22 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170217.1 | Last Updated 23 Dec 2006
Article Copyright 2006 by Astaelan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid