PHP String Compression Method Comparison Example _php tutorial

Source: Internet
Author: User
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 of compression comparison, respectively, the Chinese and English digital compression
Copy CodeThe code is as follows:

$str 1 = ' Layout 1 describes the layout, which is simply to set the size and position of the element. EXT's layout system consists of components, layouts, containers, and containers that are a special component that can manage the size and position of the component. The container is dolayout to recalculate the layout and update the DOM. 2 manual layout is unnecessary, and the framework automatically handles it for you. ';

$str 2 = '!@#$%^&* () qwertyuiopsdfghjkl!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnma!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopd:zxcvbnm#!@#!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm-!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopd$%^&* () ertyuiodfghj!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm]!@#$% ^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopdklxcvbnm@#$%^&* () rtyuiopdfghjklcvbnmfghjtyu%^& Rfghj4d56g7h8ui7h8ujirqwerqh8 ';

Echo ' Compress Chinese comparison

';
Compress_comp ($str 1, 1000); Compress 1000 times and decompress 1000 times comparison

Echo ' Comparison of compressed English numerals

';
Compress_comp ($str 2, 1000); Compress 1000 times and decompress 1000 times comparison

/* Compression */
function Compress_comp ($STR, $num) {

$func _compress = Array (' gzcompress ', ' gzencode ', ' gzdeflate ', ' bzcompress ');

Echo ' original: '. $str. '

';
Echo ' Original size: '. strlen ($str). '

';

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

';
}
}


/* Get Microtime */
function Get_microtime () {
List ($usec, $sec) = Explode (', microtime (true));
return $usec + $sec;
}
?>

Execution Result:
Copy CodeThe code is as follows:
Compress Chinese comparison

The original: Layout 1 describes the layout, simply to set the size and position of the element. EXT's layout system consists of components, layouts, containers, and containers that are a special component that can manage the size and position of the component. The container is dolayout to recalculate the layout and update the DOM. 2 manual layout is unnecessary, and the framework automatically handles it for you.

Original size: 328

gzcompress Compressed Size: 251 Time: 59.99994ms

Gzencode Compressed Size: 263 Time: 120.00012ms

Gzdeflate Compressed Size: 245 Time: 119.99989ms

Bzcompress Compressed Size: 303 Time: 259.99999ms

Comparison of compressed English numerals

Original:!@#$%^&* () qwertyuiopsdfghjkl!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnma!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopd:zxcvbnm#!@#!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm-!@#$%^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopd$%^&* () ertyuiodfghj!@#$%^&* () qwertyuiopsdfghjkl:zxcvbnm]!@#$% ^&* () ertyuiodfghjklxcvbnm@#$%^&* () rtyuiopdklxcvbnm@#$%^&* () rtyuiopdfghjklcvbnmfghjtyu%^& Rfghj4d56g7h8ui7h8ujirqwerqh8

Original size: 386

gzcompress Compressed Size: 116 Time: 50.00019ms

Gzencode Compressed Size: 128 Time: 99.9999ms

Gzdeflate Compressed Size: 110 Time: 89.99991ms

bzcompress Compressed Size: 183 Time: 210.00004ms

Can draw

The gzcompress speed is the fastest and the compression ratio is high.

Gzdeflate compression ratio is highest, speed is slightly slower than gzcompress

Gzencode and Gzdeflate Closer, gzdeflate a slight advantage

Bzcompress speed is the slowest, the compression ratio is the slowest.

It is therefore recommended to use gzcompress and gzdeflate.

http://www.bkjia.com/PHPjc/726023.html www.bkjia.com true http://www.bkjia.com/PHPjc/726023.html techarticle PHP provides a string compression method with 1.gzcompress-compress A string this function Compress the given string using the ZLIB data format. 2 . gzencode-create a gzip compressed ...

  • 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.