因为需求原因,需要多台机器向一台机器发送数据,考虑到网络原因,需要对传输的数据(json)格式进行压缩,因为是多对一,也就意味着对压缩而言,解压速度需要更好一些,以下是几种压缩算法测试的结果,一共四种LZ4,Snappy,LZF,Zlib
Original: 306
LZ4: 232
Snappy: 239
LZF: 229
Zlib: 188
Benchmark: timing 100000 iterations of LZ4 compress, LZ4 decompress, LZF compress, LZF decompress, Snappy compres, Snappy decompress, Zlib compress, Zlib uncompress...
LZ4 compress: 1 wallclock secs ( 1.03 usr + 0.01 sys = 1.04 CPU) @ 95693.78/s (n=100000)
LZ4 decompress: 0 wallclock secs ( 0.86 usr + 0.00 sys = 0.86 CPU) @ 116550.12/s (n=100000)
LZF compress: 1 wallclock secs ( 1.03 usr + 0.00 sys = 1.03 CPU) @ 97181.73/s (n=100000)
LZF decompress: 0 wallclock secs ( 0.89 usr + 0.00 sys = 0.89 CPU) @ 112485.94/s (n=100000)
Snappy compres: 1 wallclock secs ( 1.00 usr + 0.00 sys = 1.00 CPU) @ 100100.10/s (n=100000)
Snappy decompress: 0 wallclock secs ( 0.87 usr + 0.00 sys = 0.87 CPU) @ 114547.54/s (n=100000)
Zlib compress: 18 wallclock secs (17.78 usr + 0.00 sys = 17.78 CPU) @ 5622.72/s (n=100000)
Zlib uncompress: 7 wallclock secs ( 7.74 usr + 0.00 sys = 7.74 CPU) @ 12923.24/s (n=100000)
我们可以看到zlib的压缩效果是好的,但是显而易见的是压缩和解压时间也是最长的,但反过来,解压速度却相当的好,这点倒是跟模块文档里测试的结果大相径庭。所以结论就是LZ4的解压算法最符合需求,压缩的大小比Snappy要好,比LZF稍微差一点但不如Snappy差距那大,因为JSON的数据是真实环境的,也就很好衡量用哪种算法了了。
本人是win7旗舰版,i5 2500, 4G内存,perl 5.16 64位
有兴趣的可以自己测试一下。以下是测试代码
use 5.010;
use Compress::LZ4();
use Compress::Snappy();
use Compress::LZF();
use Compress::Zlib();
my $datas = ‘你自己的json数据’;
my $lz4 = Compress::LZ4::compress($datas);
my $snappy = Compress::Snappy::compress($datas);
my $lzf = Compress::LZF::compress($datas);
my $zlib = Compress::Zlib::compress($datas);
say 'Original: ' . length $datas;
say 'LZ4: ' . length $lz4;
say 'Snappy: ' . length $snappy;
say 'LZF: ' . length $lzf;
say 'Zlib: ' . length $zlib;
use Benchmark;
Benchmark::timethese 100000, {
'LZ4 compress' => sub {
Compress::LZ4::compress($datas);
},
'Snappy compres' => sub {
Compress::Snappy::compress($datas);
},
'LZF compress' => sub {
Compress::LZF::compress($datas);
},
'Zlib compress' => sub {
Compress::Zlib::compress($datas);
},
'LZ4 decompress' => sub {
Compress::LZ4::decompress($lz4);
},
'Snappy decompress' => sub {
Compress::Snappy::decompress($snappy);
},
'LZF decompress' => sub {
Compress::LZF::decompress($lzf);
},
'Zlib uncompress' => sub {
Compress::Zlib::uncompress($zlib);
}
};