php string compression method comparison example
- 2020-12-16 05:53:52
- OfStack
php provides string compression methods such as
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.
bzcompress - Compacts 1 string into bzip2 encoded data
bzcompress() compacts the specified string and returns the data encoded in bzip2.
The following four methods are used to compress and compare Chinese and English numbers respectively
Execution Results:
We can draw
gzcompress is the fastest and has a higher compression ratio.
gzdeflate had the highest compression ratio and was slightly slower than gzcompress
gzencode is close to gzdeflate, while gzdeflate has a slight advantage
bzcompress has the slowest speed and the slowest compression ratio.
Therefore, gzcompress and gzdeflate are recommended.
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.
bzcompress - Compacts 1 string into bzip2 encoded data
bzcompress() compacts the specified string and returns the data encoded in bzip2.
The following four methods are used to compress and compare Chinese and English numbers respectively
<?php
$str1 = ' layout 1 introduce Layout, in simple terms, sets the size and location of elements. Ext The layout system includes components, layouts, containers, and containers 1 A special component that manages the size and location of components. The container is passed through doLayout To recalculate the layout and update it DOM. 2 Manual layout is not necessary, the framework will do it for you automatically. ';
$str2 = '!@#$%^&*()QWERTYUIOPSDFGHJKL!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNMa!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPD:ZXCVBNM#!@#!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM-!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPD$%^&*()ERTYUIODFGHJ!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM]!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPDKLXCVBNM@#$%^&*()RTYUIOPDFGHJKLCVBNMFGHJTYU%^&RFGHJ4d56g7h8ui7h8ujirqwerqh8';
echo '<b> Compressed Chinese comparison </b><br><br>';
compress_comp($str1, 1000); // The compression 1000 time with unzip 1000 Time to compare
echo '<b> Compress English numerical comparison </b><br><br>';
compress_comp($str2, 1000); // The compression 1000 time with unzip 1000 Time to compare
/* The compression */
function compress_comp($str, $num){
$func_compress = array('gzcompress', 'gzencode', 'gzdeflate', 'bzcompress');
echo ' The original :'.$str.'<br><br>';
echo ' The 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>';
}
}
/* To obtain microtime */
function get_microtime(){
list($usec, $sec) = explode(' ', microtime(true));
return $usec+$sec;
}
?>
Execution Results:
Compressed Chinese comparison
The original : layout 1 introduce Layout, in simple terms, sets the size and location of elements. Ext The layout system includes components, layouts, containers, and containers 1 A special component that manages the size and location of components. The container is passed through doLayout To recalculate the layout and update it DOM. 2 Manual layout is not necessary, the framework will do it for you automatically.
The original size :328
gzcompress Compressed size :251 Time consuming :59.99994ms
gzencode Compressed size :263 Time consuming :120.00012ms
gzdeflate Compressed size :245 Time consuming :119.99989ms
bzcompress Compressed size :303 Time consuming :259.99999ms
Compress English numerical comparison
The original :!@#$%^&*()QWERTYUIOPSDFGHJKL!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNMa!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPD:ZXCVBNM#!@#!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM-!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPD$%^&*()ERTYUIODFGHJ!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM]!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPDKLXCVBNM@#$%^&*()RTYUIOPDFGHJKLCVBNMFGHJTYU%^&RFGHJ4d56g7h8ui7h8ujirqwerqh8
The original size :386
gzcompress Compressed size :116 Time consuming :50.00019ms
gzencode Compressed size :128 Time consuming :99.9999ms
gzdeflate Compressed size :110 Time consuming :89.99991ms
bzcompress Compressed size :183 Time consuming :210.00004ms
We can draw
gzcompress is the fastest and has a higher compression ratio.
gzdeflate had the highest compression ratio and was slightly slower than gzcompress
gzencode is close to gzdeflate, while gzdeflate has a slight advantage
bzcompress has the slowest speed and the slowest compression ratio.
Therefore, gzcompress and gzdeflate are recommended.