Java常用的压缩方式是gzip,它的性能比另外一种zip压缩方式好多少呢,抽空写了个程序来验证一下
分别用以下长度的字节字符串进行压缩比较
int[] intArr = { 1, 10, 50, 100, 200, 300, 500, 700, 900, 1000, 2000, 5000, 10000, 100000, 1000000 };
测试结果如下
// 压缩大小分析
origin=1,gzip=21,zip=123
origin=10,gzip=30,zip=132
origin=50,gzip=70,zip=172
origin=100,gzip=119,zip=221
origin=200,gzip=194,zip=296
origin=300,gzip=269,zip=371
origin=500,gzip=420,zip=522
origin=700,gzip=568,zip=670
origin=900,gzip=719,zip=821
origin=1000,gzip=790,zip=892
origin=2000,gzip=1535,zip=1637
origin=5000,gzip=3787,zip=3889
origin=10000,gzip=7540,zip=7642
origin=100000,gzip=75205,zip=75307
origin=1000000,gzip=752046,zip=752148
// 压缩率分析
origin=1,gzip=2100.00%,zip=12300.00%
origin=10,gzip=300.00%,zip=1320.00%
origin=50,gzip=140.00%,zip=344.00%
origin=100,gzip=119.00%,zip=221.00%
origin=200,gzip=97.00%,zip=148.00%
origin=300,gzip=89.67%,zip=123.67%
origin=500,gzip=84.00%,zip=104.40%
origin=700,gzip=81.14%,zip=95.71%
origin=900,gzip=79.89%,zip=91.22%
origin=1000,gzip=79.00%,zip=89.20%
origin=2000,gzip=76.75%,zip=81.85%
origin=5000,gzip=75.74%,zip=77.78%
origin=10000,gzip=75.40%,zip=76.42%
origin=100000,gzip=75.21%,zip=75.31%
origin=1000000,gzip=75.20%,zip=75.21%
// 压缩时间分析
origin=1,gzip=58ms,zip=2ms
origin=10,gzip=0ms,zip=0ms
origin=50,gzip=0ms,zip=0ms
origin=100,gzip=1ms,zip=0ms
origin=200,gzip=0ms,zip=0ms
origin=300,gzip=0ms,zip=1ms
origin=500,gzip=0ms,zip=0ms
origin=700,gzip=0ms,zip=1ms
origin=900,gzip=0ms,zip=0ms
origin=1000,gzip=0ms,zip=1ms
origin=2000,gzip=0ms,zip=0ms
origin=5000,gzip=1ms,zip=0ms
origin=10000,gzip=1ms,zip=0ms
origin=100000,gzip=5ms,zip=5ms
origin=1000000,gzip=52ms,zip=55ms
分析与结论
1)内容字节数偏小时,不适合进行压缩,压缩内容反而会更大
2)gzip压缩率大于zip,随着字节数增大,zip压缩率趋近与gzip
3)gzip与zip压缩时间差别不大
因此实际使用中大都采用gzip