SoFunction
Updated on 2025-03-09

PHP string compression method comparison example

The string compression methods provided by php are

— Compress a string

This function compress the given string using the ZLIB data format.

— Create a gzip compressed string

This function returns a compressed version of the input data compatible with the output of the gzip program

— Deflate a string

This function compress the given string using the DEFLATE data format.

— Compress a string into bzip2 encoded data

bzcompress() compresses the specified string and returns the data with bzip2 encoding.

The following is a compression comparison of these four methods, and the Chinese and English numbers are compressed respectively.
Copy the codeThe code is as follows:

<?php

$str1 = 'Layout 1 Introduction Layout, simply put, set the size and position of the element. Ext's layout system includes components, layouts, containers. Containers are special components that can manage the size and location of components. The container is recalculated through doLayout and updated with DOM. 2 Manual layout is unnecessary and the framework will automatically handle it for you. ';

$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); // Compare 1000 times to decompress 1000 times

echo '<b>Comparison of compressed English numbers</b><br><br>';
compress_comp($str2, 1000); // Compare 1000 times to decompress 1000 times

/* Compression */
function compress_comp($str, $num){

$func_compress = array('gzcompress', 'gzencode', 'gzdeflate', 'bzcompress');

echo 'Original text:'.$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); // Decompress 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); // Decompress method: bzdecompress
break;
}
}
$endtime = get_microtime();
echo $func_compress[$i].' Compressed size:'.strlen($mstr).' Time to consume:'.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 the codeThe code is as follows:

Compressed Chinese comparison

Original text: Layout 1 Introduction Layout, simply put, set the size and position of the element. Ext's layout system includes components, layouts, containers. Containers are special components that can manage the size and location of components. The container is recalculated through doLayout and updated with DOM. 2 Manual layout is unnecessary and the framework will automatically handle it for you.

Original size: 328

gzcompress size after compression: 251 Time to consume: 59.99994ms

gzencode compressed size: 263 Time to consume: 120.00012ms

gzdeflate compressed size: 245 Time consumed: 119.99989ms

bzcompress size after compression: 303 Time to consume: 259.99999ms

Comparison of compressed English numbers

Original text:!@#$%^&*()QWERTYUIOPSDFGHJKL!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNMa!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPD:ZXCVBNM#!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM-!@#$%^&*()ERTYUIODFGHJKLXCVBNM @#$%^&*()RTYUIOPD$%^&*()ERTYUIODFGHJ!@#$%^&*()QWERTYUIOPSDFGHJKL:ZXCVBNM]!@#$%^&*()ERTYUIODFGHJKLXCVBNM@#$%^&*()RTYUIOPDKLXCVBNM@#$%^&*()RTYUIOPDFGHJKLCVBNMFGHJTYU%^&RFGHJ4d56g7h8ui7h8ujirqwerqh8

Original size: 386

gzcompress size after compression: 116 Time to take: 50.00019ms

gzencode compressed size: 128 Time to take: 99.9999ms

gzdeflate compressed size: 110 Time to take: 89.99991ms

bzcompress size after compression: 183 Time to consume: 210.00004ms

Can be concluded

gzcompress has the fastest speed and a higher compression ratio.

gzdeflate has the highest compression ratio and is slightly slower than gzcompress

gzencode is closer to gzdeflate, and gzdeflate has a slight advantage

bzcompress is the slowest and the compression ratio is the slowest.

Therefore, it is recommended to use gzcompress and gzdeflate.