File compression
Files can get quite large. As we have already discussed, file IO and network IO take a long time, especially compared to CPUs’ speeds. Anything we can do to minimize the time it takes to read from or write to IO could be worth it. This is even true if it means that we must make the CPU do a lot more. Of course, you need to measure this and see whether that also applies in your situation, but sometimes, sacrificing CPU time to speed up IO can make a huge difference.
One of the ways to do this is by limiting the amount of data we write in a file or a network stream. That can be done using compression.
In the CLR, you have a choice. You can use DeflateStream
or GZipStream
to do this. GZipStream
uses DeflateStream
internally, so DeflateStream
is obviously faster. GZipStream
, however, produces compressed files that can be read by external software. GZip is a standardized compression algorithm.
Compressing some data
Let’s compress a string using...