使用gzencode对数据进行压缩

对于上一篇博客要存储blob的类型,可以对数据进行压缩后存储
函数如下:http://php.net/manual/zh/function.gzencode.php
简单的例子

        $str = "this is a test string this is a test string this is a test string this is a test string this is a test string";
        var_dump($str);
        $zip   = gzencode($str);
        $unzip = gzdecode($zip);
        var_dump($zip);
        var_dump($unzip);

结果:
string(109) “this is a test string this is a test string this is a test string this is a test string this is a test string” string(44) “-� +��,V�D���� �⒢̼t� ڈ ��� m” string(109) “this is a test string this is a test string this is a test string this is a test string this is a test string”
可以看到明显的数据压缩。
对于存储在KV或者数据库的数据压缩后存储,虽然浪费了CPU性能,但是大大提高了存储空间,在一般的接口速度上也能提高。
this is a benchmark test of gzencode (.txt file)
———————————————-
original file size = 3.29 MB (3,459,978 bytes)
compress lvl 1 = 1.09 MB (1,144,006 bytes)
compress lvl 2 = 1.06 MB (1,119,518 bytes)
compress lvl 3 = 1.03 MB (1,085,567 bytes)
compress lvl 4 = 953 KB (976,538 bytes)
compress lvl 5 = 909 KB (931,486 bytes)
compress lvl 6 = 910 KB (932,516 bytes)
compress lvl 7 = 910 KB (932,608 bytes)
compress lvl 8 = 910 KB (932,646 bytes)
compress lvl 9 = 910 KB (932,652 bytes)

发表评论

邮箱地址不会被公开。 必填项已用*标注


*