PHP 对数组进行压缩编码, 哪种最好?(php几种压缩方式的对比)

来源:互联网 发布:淘宝开店协议无法同意 编辑:程序博客网 时间:2024/05/29 13:28
<?php@set_time_limit(0);if(php_sapi_name()!=='cli') {    header('Content-Type:text/plain');}$s = file_get_contents('http://www.oschina.net/question/998019_121505'); $data = array('data'=>str_repeat($s,100));  function benchmark($function, $times=1){    $started_at = microtime(1);    $data = null;    for($i=0; $i<$times; $i++){        $data = $function();    }         printf("%.5fs, length:%.5fm\n\n", microtime(1)-$started_at, (strlen($data) / 1024 /1024));}  echo "serialize \n"; benchmark(function() use($data){    $t = ((serialize($data)));    $s = unserialize((($t)));    return $t;}); echo "serialize + base64 \n"; benchmark(function() use($data){    $t = base64_encode((serialize($data)));    $s = unserialize((base64_decode($t)));    return $t;}); echo "serialize + gzip \n"; benchmark(function() use($data){    $t = (gzcompress(serialize($data)));    $s = unserialize(gzuncompress(($t)));    return $t;}); echo "serialize+base64_encode +gzip \n";benchmark(function() use($data){    $t = base64_encode(gzcompress(serialize($data)));    $s = unserialize(gzuncompress(base64_decode($t)));    return $t;});exit();


运行结果:

 返回值:serialize 0.01427s, length:6.02410mserialize + base64 0.17287s, length:8.03214mserialize + gzip 0.43907s, length:1.44310mserialize+base64_encode +gzip 0.51364s, length:1.92414m感觉各有优势, 不知道选择哪种方案来做...要么时间慢, 要么容量大, 暂没有即时间快,又容量小的方案,权衡下,选择合适的来用。文章来源于:开源中国社区:http://www.oschina.net/question/998019_121505 


0 0