Comparison of php string compression methods

Source: Internet
Author: User

There are many string compression methods provided by php. The following describes the php string compression methods. For more information, see

Php provides the following string compression methods: 1.gz compress-Compress a string This function compress the given string using the ZLIB data format. 2.gz encode-Create a gzip compressed string This function returns a compressed version of the input data compatible with the output of the gzip program 3.gz deflate-Deflate a string This function compress the given string using DEFLATE data format. 4. bzcompress-compress a string into bzip 2. The encoded data bzcompress () compresses the specified string and returns data in bzip2 encoding. The following code compresses and copies Chinese and English numbers in the following four methods: <? Php $ str1 = 'layout 1 introduces 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> compare compressed Chinese characters </B> <br>'; compress_comp ($ str1, 1000 ); // compress_comp ($ str2, 1000 ); // compare 1000 compression times with 1000 decompression times/* compression */function compress_comp ($ str, $ num) {$ func_compress = array ('gzcompress', 'gzencode ', 'gzdeflate', 'bzc Ompress '); echo 'original :'. $ str. '<br>'; echo 'original 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 = g Zdeflate ($ str, 9); // decompression method: gzinflate break; case 'bzcompress ': $ 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 the Code as follows: compress the Chinese text to compare the original text: Layout 1 introduces the layout, simply 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. Original Article Size: 328 size after gzcompress compression: 251 elapsed time: 59.99994 ms after gzencode compression size: 263 elapsed time: 120.00012 ms after gzdeflate compression size: 245 elapsed time: 119.99989 ms bzcompress after compression size: 303 time consumption: 259.99999 ms compression english numbers comparison Original :! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNMa! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPD: ZXCVBNM #! @#! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNM -! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPD $ % ^ & * () ERTYUIODFGHJ! @ # $ % ^ & * () QWERTYUIOPSDFGHJKL: ZXCVBNM]! @ # $ % ^ & * () ERTYUIODFGHJKLXCVBNM @ # $ % ^ & * () RTYUIOPDKLXCVBNM @ # $ % ^ & * () rtyuio1_ghjklcvbnmfghjtyu % ^ & proportion original size: 386 size after gzcompress compression: 116 elapsed time: 50.00019 ms after gzencode compression size: 128 elapsed time: 99.9999 ms after gzdeflate compression size: 110 elapsed time: 89.99991 ms after bzcompress compression size: 183 elapsed time: 210.00004 ms, it can be concluded that gzcompress is the fastest and the compression ratio is high. The gzdeflate compression ratio is the highest, and the speed is slightly slower than that of gzcompress gzencode and gzdeflate. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.