3.性能测试
  针对1M数据分别调用compress压缩和uncompress解压缩,循环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 ",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 ",1000.0*(end-start)/CLOCKS_PER_SEC);
delete [] data;
}
}
int main(int argc, char **argv)
{
testCompress();
testunCompress();
return 0;
}
  测试结果:
time used(ms):470.00
time used(ms):40.00
  4.总结
  zlib压缩1M数据耗时47ms左右,解压缩4ms左右。解压非常快。