Flash Player 11.3添加了一个压缩和解压ByteArray的新方法:LZMA算法。这个算法很有用,因为和现存的zlib和deflate算法相比,LZMA通常能压缩到更小的程度。但这会导致压缩速度减少多少呢?今天这篇文章就是为了找出这个问题的答案。
首先,如果你感兴趣的话,可以很容易切换到LZMA。所有你需要做的就是将CompressionAlgorithm.LZMA传递给ByteArray.compress或ByteArray.uncompress。当然你还需要建立好Flash Player 11.3,并且保证你的用户至少在使用这个版本。
至于你将得到的额外压缩,这取决于你要压缩的数据,但大约比gzip要好50%,可以有效地减少下载时间!因此,在压缩速率这篇文章的测试应用程序的基础上,我们通过下面的测试程序来看看LZMA与zlib和Deflate性能上的区别:
package { import flash.display.*; import flash.utils.*; import flash.text.*; public class CompressionSpeedFollowup extends Sprite { private var logger:TextField = new TextField(); private function row(...cols): void { logger.appendText(cols.join(",") + ""); } public function CompressionSpeedFollowup() { logger.autoSize = TextFieldAutoSize.LEFT; addChild(logger); row( "Size", "deflate (compress)", "zlib (compress)", "LZMA (compress)", "deflate (uncompress)", "zlib (uncompress)", "LZMA (compress)" ); runTests("1 KB", 1024, 1024); runTests("1 MB", 1024*1024, 1); } private function runTests(label:String, size:int, reps:int): void { var beforeTime:int; var afterTime:int; var emptyTime:int; var deflateTimeCompress:int; var zlibTimeCompress:int; var lzmaTimeCompress:int; var deflateTimeUncompress:int; var zlibTimeUncompress:int; var lzmaTimeUncompress:int; var bytes:ByteArray = new ByteArray(); var originalBytes:ByteArray = new ByteArray(); var compressedBytes:ByteArray = new ByteArray(); var i:int; var zlib:String = CompressionAlgorithm.ZLIB; var deflate:String = CompressionAlgorithm.DEFLATE; var lzma:String = CompressionAlgorithm.LZMA; fillBytes(originalBytes, size); // Empty beforeTime = getTimer(); for (i = 0; i < reps; ++i) { copyBytes(originalBytes, bytes); } afterTime = getTimer(); emptyTime = afterTime - beforeTime; // Compress beforeTime = getTimer(); for (i = 0; i < reps; ++i) { copyBytes(originalBytes, bytes); bytes.compress(deflate); } afterTime = getTimer(); deflateTimeCompress = afterTime - beforeTime - emptyTime; beforeTime = getTimer(); for (i = 0; i < reps; ++i) { copyBytes(originalBytes, bytes); bytes.compress(zlib); } afterTime = getTimer(); zlibTimeCompress = afterTime - beforeTime - emptyTime; beforeTime = getTimer(); for (i = 0; i < reps; ++i) { copyBytes(originalBytes, bytes); bytes.compress(lzma); } afterTime = getTimer(); lzmaTimeCompress = afterTime - beforeTime - emptyTime; // Uncompress copyBytes(originalBytes, compressedBytes); compressedBytes.compress(deflate); beforeTime = getTimer(); for (i = 0; i < reps; ++i) { copyBytes(compressedBytes, bytes); bytes.uncompress(deflate); } afterTime = getTimer(); deflateTimeUncompress = afterTime - beforeTime - emptyTime; copyBytes(originalBytes, compressedBytes); compressedBytes.compress(zlib); beforeTime = getTimer(); for (i = 0; i < reps; ++i) { copyBytes(compressedBytes, bytes); bytes.uncompress(zlib); } afterTime = getTimer(); zlibTimeUncompress = afterTime - beforeTime - emptyTime; copyBytes(originalBytes, compressedBytes); compressedBytes.compress(lzma); beforeTime = getTimer(); for (i = 0; i < reps; ++i) { copyBytes(compressedBytes, bytes); bytes.uncompress(lzma); } afterTime = getTimer(); lzmaTimeUncompress = afterTime - beforeTime - emptyTime; row( label, deflateTimeCompress, zlibTimeCompress, lzmaTimeCompress, deflateTimeUncompress, zlibTimeUncompress, lzmaTimeUncompress ); } private function fillBytes(bytes:ByteArray, size:int): void { bytes.length = 0; bytes.position = 0; for (var i:int; i < size; ++i) { bytes.writeByte(Math.random()*256); } bytes.position = 0; } private function copyBytes(bytes:ByteArray, into:ByteArray): void { bytes.position = 0; into.position = 0; into.length = 0; into.writeBytes(bytes); bytes.position = 0; into.position = 0; } } }
我是在以下环境中运行的测试应用程序:
· Flex SDK(MXMLC)4.6.0.23201,在发布模式下编译(没有调试或冗长的堆栈跟踪)
· Flash Player 11.4.402.287
· 2.3 GHz的英特尔酷睿i7处理器
· Mac OS X 10.8.2
下面是我得到的结果:(最下面的LZMA标签改为LZMA(uncompress))
正如图中所描述的,LZMA的压缩和解压速率要比zlib和Deflate慢很多。大约压缩慢了28倍,解压慢了16倍,至少我使用的随机测试数据是这样。然而,与大多数编程任务一样,这里还是有个权衡的。如果你需要提高下载速度的话,你现在就可以使用LZMA压缩,但下载之后就要花费额外的处理能力来解压。这可能是你愿意做的一个权衡了,但你一定要注意上面的结果:你的CPU将遇到麻烦了!