Php provides the following string compression methods:
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-compress a string into bzip2 encoded data
Bzcompress () compresses the specified string and returns data in bzip2 encoding.
The following four methods are used for compression and comparison, and Chinese and English numbers are compressed respectively.
Copy codeThe Code is as follows:
<? Php
$ Str1 = 'layout 1 describes the layout, which is simply to set the size and position of the element. The Layout System of Ext includes components, layout, containers, and containers. It is a special component that allows you to manage the size and position of components. The container recalculates the layout through doLayout and updates the DOM. 2 manual layout. The framework will automatically process the layout for you. ';
$ Str2 = '! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNMa! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPD: ZXCVBNM #! @#! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNM -! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPD $ % ^ & * () ERTYUIODFGHJ! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNM]! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPDKLXCVBNM @ # $ % ^ & * () rtyuio1_ghjklcvbnmfghjtyu % ^ & RFGHJ4d56g7h8ui7h8ujirqwerqh8 ';
Echo '<B> Chinese Character compression comparison </B> <br> ';
Compress_comp ($ str1, 1000); // compress 1000 times and decompress 1000 times
Echo '<B> compress english numbers </B> <br> ';
Compress_comp ($ str2, 1000); // compress 1000 times and decompress 1000 times
/* Compression */
Function compress_comp ($ str, $ num ){
$ Func_compress = array ('gzcompress ', 'gzencode', 'gzdeflate', 'bzcompress ');
Echo 'original: '. $ str.' <br> ';
Echo 'original text size: '. strlen ($ str).' <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 'bzcompus ':
$ Mstr = bzcompress ($ str, 9); // decompression method: bzdecompress
Break;
}
}
$ Endtime = get_microtime ();
Echo $ func_compress [$ I]. 'size after compression :'. strlen ($ mstr ). 'Time consumed :'. round ($ endtime-$ starttime ). 'Ms <br> ';
}
}
/* Get microtime */
Function get_microtime (){
List ($ usec, $ sec) = explode ('', microtime (true ));
Return $ usec + $ sec;
}
?>
Execution result:
Copy codeThe Code is as follows:
Compressed Chinese Comparison
Original article: Layout 1 describes the layout, which is simply to set the size and position of elements. The Layout System of Ext includes components, layout, containers, and containers. It is a special component that allows you to manage the size and position of components. The container recalculates the layout through doLayout and updates the DOM. 2 manual layout. The framework will automatically process the layout for you.
Original Article Size: 328
Size after gzcompress compression: 251 elapsed time: 59.99994 ms
Size after gzencode compression: 263 elapsed time: 120.00012 ms
Size after gzdeflate compression: 245 elapsed time: 119.99989 ms
Size after bzcompress compression: 303 elapsed time: 259.99999 ms
Compress english numbers
Original article :! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNMa! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPD: ZXCVBNM #! @#! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNM -! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPD $ % ^ & * () ERTYUIODFGHJ! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNM]! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPDKLXCVBNM @ # $ % ^ & * () rtyuio1_ghjklcvbnmfghjtyu % ^ & Others
Original Article Size: 386
Size after gzcompress compression: 116 elapsed time: 50.00019 ms
Size after gzencode compression: 128 elapsed time: 99.9999 ms
Size after gzdeflate compression: 110 elapsed time: 89.99991 ms
Size after bzcompress compression: 183 elapsed time: 210.00004 ms
We can conclude that
Gzcompress provides the fastest speed and a high compression ratio.
Gzdeflate has the highest compression ratio and is slightly slower than gzcompress.
Gzencode is close to gzdeflate, and gzdeflate has a slight advantage.
Bzcompress is the slowest, and the compression ratio is the slowest.
Therefore, we recommend that you use gzcompress and gzdeflate.