PHP String Compression Method Comparison example _php tips

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 for compression comparison, respectively, Chinese and English digital compression
Copy Code code as follows:

<?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); Compression 1000 times and decompression 1000 times comparison

echo ' <b> compressed English numeral 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 (' gzcompress ', ' 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;
}
?>

Execution results:
Copy Code code as follows:

Compress Chinese Comparisons

Original: Layout 1 describes the layout, simply 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 after compression size: 251 time consuming: 59.99994ms

Gzencode after compression size: 263 time consuming: 120.00012ms

Gzdeflate after compression size: 245 time consuming: 119.99989ms

Bzcompress after compression size: 303 time consuming: 259.99999ms

Compressed English numeral comparison

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

Original size: 386

Gzcompress after compression size: 116 time consuming: 50.00019ms

Gzencode after compression size: 128 Time consuming: 99.9999ms

Gzdeflate after compression 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.

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.