qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問(wèn) http://qaseven.github.io/

          zlib使用與性能測(cè)試

           zlib作為最常用的壓縮工具,本文對(duì)其使用進(jìn)行簡(jiǎn)單說(shuō)明,并進(jìn)行一個(gè)簡(jiǎn)單的性能測(cè)試
            1.下載編譯
            可以從zlib官網(wǎng)下載:http://www.zlib.net/
            下載后直接make既可。make后再目錄下生成libz.a.
            2.使用
            引用zlib.h和libz.a既可。關(guān)鍵在于zlib.h,它提供了一些函數(shù)。
            以下是引自“http://www.cppblog.com/woaidongmao/archive/2009/09/07/95495.html”的關(guān)于zlib.h的說(shuō)明:
            都在zlib.h中,看到一堆宏不要暈,其實(shí)都是為了兼容各種編譯器和一些類型定義.死死抓住那些主要的函數(shù)的原型聲明就不會(huì)受到這些東西的影響了.
            關(guān)鍵的函數(shù)有那么幾個(gè):
            (1)int compress (Bytef *dest,   uLongf *destLen, const Bytef *source, uLong sourceLen);
            把源緩沖壓縮成目的緩沖, 就那么簡(jiǎn)單, 一個(gè)函數(shù)搞定
            (2) int compress2 (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen,int level);
            功能和上一個(gè)函數(shù)一樣,都一個(gè)參數(shù)可以指定壓縮質(zhì)量和壓縮數(shù)度之間的關(guān)系(0-9)不敢肯定這個(gè)參數(shù)的話不用太在意它,明白一個(gè)道理就好了: 要想得到高的壓縮比就要多花時(shí)間
            (3) uLong compressBound (uLong sourceLen);
            計(jì)算需要的緩沖區(qū)長(zhǎng)度. 假設(shè)你在壓縮之前就想知道你的產(chǎn)度為 sourcelen 的數(shù)據(jù)壓縮后有多大, 可調(diào)用這個(gè)函數(shù)計(jì)算一下,這個(gè)函數(shù)并不能得到精確的結(jié)果,但是它可以保證實(shí)際輸出長(zhǎng)度肯定小于它計(jì)算出來(lái)的長(zhǎng)度
            (4) int uncompress (Bytef *dest,   uLongf *destLen,const Bytef *source, uLong sourceLen);
            解壓縮(看名字就知道了:)
            (5) deflateInit() + deflate() + deflateEnd()
            3個(gè)函數(shù)結(jié)合使用完成壓縮功能,具體用法看 example.c 的 test_deflate()函數(shù). 其實(shí) compress() 函數(shù)內(nèi)部就是用這3個(gè)函數(shù)實(shí)現(xiàn)的(工程 zlib 的 compress.c 文件)
            (6) inflateInit() + inflate() + inflateEnd()
            和(5)類似,完成解壓縮功能.
            (7) gz開頭的函數(shù).用來(lái)操作*.gz的文件,和文件stdio調(diào)用方式類似. 想知道怎么用的話看example.c 的 test_gzio() 函數(shù),很easy.
            (8) 其他諸如獲得版本等函數(shù)就不說(shuō)了.
            總結(jié):其實(shí)只要有了compress() 和uncompress() 兩個(gè)函數(shù),在大多數(shù)應(yīng)用中就足夠了. 3.性能測(cè)試
            針對(duì)1M數(shù)據(jù)分別調(diào)用compress壓縮和uncompress解壓縮,循環(huán)10次。
            代碼如下:
          #include <stdio.h>
          #include <time.h>
          #include "zlib.h"
          const int MAX_BUFFER_SIZE = 1024*1024*4;
          unsigned char DATA_BUFFER[MAX_BUFFER_SIZE];
          void testCompress()
          {
          const char * file = "/tmp/e2.txt.backup";
          FILE *f1 = fopen(file,"r");
          if(f1)
          {
          fseek(f1,0,2);
          int len = ftell(f1);
          fseek(f1,0,0);
          char * data = new char[len];
          fread(data,1,len,f1);
          fclose(f1);
          //uLong dst_len = MAX_BUFFER_SIZE;
          //Bytef * dst = (Bytef*)DATA_BUFFER;
          clock_t start = clock();
          for(int i=0; i<10; i++)
          {
          uLong dst_len = MAX_BUFFER_SIZE;
          Bytef * dst = (Bytef*)DATA_BUFFER;
          compress(dst,&dst_len,(Bytef *)data,(uLong)len);
          }
          clock_t end = clock();
          printf("time used(ms):%.2f\n",1000.0*(end-start)/CLOCKS_PER_SEC);
          delete [] data;
          }
          }
          void testunCompress()
          {
          const char * file = "/tmp/2.gz";
          FILE *f1 = fopen(file,"r");
          if(f1)
          {
          fseek(f1,0,2);
          int len = ftell(f1);
          fseek(f1,0,0);
          char * data = new char[len];
          fread(data,1,len,f1);
          fclose(f1);
          //uLong dst_len = MAX_BUFFER_SIZE;
          //Bytef * dst = (Bytef*)DATA_BUFFER;
          clock_t start = clock();
          for(int i=0; i<10; i++)
          {
          uLong dst_len = MAX_BUFFER_SIZE;
          Bytef * dst = (Bytef*)DATA_BUFFER;
          uncompress(dst,&dst_len,(Bytef *)data,(uLong)len);
          }
          clock_t end = clock();
          printf("time used(ms):%.2f\n",1000.0*(end-start)/CLOCKS_PER_SEC);
          delete [] data;
          }
          }
          int main(int argc, char **argv)
          {
          testCompress();
          testunCompress();
          return 0;
          }
            測(cè)試結(jié)果:
          time used(ms):470.00
          time used(ms):40.00
            4.總結(jié)
            zlib壓縮1M數(shù)據(jù)耗時(shí)47ms左右,解壓縮4ms左右。解壓非常快。

          posted on 2014-01-20 10:01 順其自然EVO 閱讀(636) 評(píng)論(0)  編輯  收藏 所屬分類: 性能測(cè)試

          <2014年1月>
          2930311234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 图木舒克市| 仙桃市| 津南区| 昌吉市| 特克斯县| 延川县| 九台市| 永州市| 唐山市| 浦城县| 泗水县| 武定县| 开鲁县| 虞城县| 洞头县| 长乐市| 五常市| 微山县| 开鲁县| 康平县| 海兴县| 镶黄旗| 台中市| 资源县| 福贡县| 宿州市| 司法| 察雅县| 梁山县| 云林县| 平舆县| 香港 | 新平| 安达市| 盐山县| 确山县| 简阳市| 吉林市| 阿拉善左旗| 台湾省| 隆德县|