The string compression method provided by PHP has
1.gzcompress-compress a string
This function compress the given string using the ZLIB data format.
2.gzencode-create a gzip compressed string
This function returns a compressed version of the "Input data compatible" with the output of the "gzip program"
3.gzdeflate-deflate a string
This function compress the given string using the DEFLATE data format.
4.bzcompress-to compress a string into BZIP2 encoded data
Bzcompress () compresses the specified string and returns the data in BZIP2 encoding.
The following four methods for compression comparison, respectively, Chinese and English digital compression
<?php $str 1 = ' Layout 1 ' describes the layout, simply to set the size and position of the element. EXT's layout system includes components, layouts, containers, and containers that are a special component that can manage the size and location of components. The container is dolayout to recalculate the layout and update the DOM. 2 manual layout is unnecessary and the framework will be automatically processed for you.
'; $str 2 = '!@#$%^&* () qwertyuiopsdfghjkl!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnma!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopd:zxcvbnm#!@#!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm-!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopd$%^&* () ertyuiodfghj!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm]!@#$% ^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopdklxcvbnm@#$%^&* () rtyuiopdfghjklcvbnmfghjtyu%^&
Rfghj4d56g7h8ui7h8ujirqwerqh8 ';
Echo ' <b> compress Chinese comparison </b><br><br> '; Compress_comp ($str 1, 1000);
Compress 1000 times and decompress 1000 times compare echo ' <b> compress English digit comparison </b><br><br> '; Compress_comp ($str 2, 1000); Compression 1000 times and decompression 1000 times comparison * * Compression/function Compress_comp ($STR, $num) {$func _compress = array (' GZCOMPR
Ess ', ' gzencode ', ' gzdeflate ', ' bzcompress ');
Echo ' original: '. $str. ' <br><br> '; Echo ' Original size: '. strlen ($str). '
<br><br> ';
For ($i =0, $length =count ($func _compress); $i < $length; $i + +) {$starttime = Get_microtime ();
For ($j =0 $j < $num; $j + +) {switch ($func _compress[$i]) {case ' gzcompress ': $mstr = Gzcompress ($str, 9);
Decompression method: Gzuncompress break; Case ' Gzencode ': $mstr = Gzencode ($str, 9);
Decompression method: Gzdecode php>=5.4 break; Case ' gzdeflate ': $mstr = Gzdeflate ($str, 9);
Decompression method: Gzinflate break; Case ' bzcompress ': $mstr = bzcompress ($str, 9);
Decompression method: Bzdecompress break;
}} $endtime = Get_microtime (); echo $func _compress[$i]. ' Compressed size: '. strlen ($MSTR). ' Time consuming:'. Round (($endtime-$starttime) *1000,5). '
Ms<br><br> '; \ * * Get microtime/function Get_microtime () {list ($usec, $sec) = Explode (", microtime (True))
;
return $usec + $sec; }?>
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/PHP/
Execution results:
Compressed Chinese comparison original: Layout 1 describes the layout, in simple terms, is to set the size and location of the element. EXT's layout system includes components, layouts, containers, and containers that are a special component that can manage the size and location of components.
The container is dolayout to recalculate the layout and update the DOM. 2 manual layout is unnecessary and the framework will be automatically processed for you. Original size: 328 gzcompress Compressed Size: 251 time consuming: 59.99994ms Gzencode compressed after size: 263 time consuming: 120.00012ms gzdeflate Compressed Size: 2 45 time consuming: 119.99989MS bzcompress Compressed Size: 303 time consuming: 259.99999ms compressed English numeral comparison original:!@#$%^&* () qwertyuiopsdfgh jkl!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnma!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () RTYUIOPD:ZXCVBNM#!@ #!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm-!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () RTYUIOPD$%^&* () ertyuiodfghj!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm]!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopdklxcvbnm@#$%^&* () Rtyuiopdfghjklcvbnmfghjtyu%^&rfghj4d56g7h8ui7h8ujirqwerqh8 Original size: 386 GZC
Ompress size after compression: 116 time: 50.00019MS Gzencode compressed after size: 128 time consuming: 99.9999MS gzdeflate compressed after size: 110 time consuming: 89.99991ms Bzcompress after compression size: 183 time consuming: 210.00004Ms
Can draw
The gzcompress speed is the fastest and the compression ratio is high.
Gzdeflate Compression ratio is the highest, slightly slower than gzcompress
Gzencode and Gzdeflate Closer, gzdeflate a slight advantage
Bzcompress speed is the slowest, compression rate is the slowest.
It is therefore recommended that gzcompress and gzdeflate be used.