testzlib.c
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <windows.h>
  4. #include "zlib.h"
  5. void MyDoMinus64(LARGE_INTEGER *R,LARGE_INTEGER A,LARGE_INTEGER B)
  6. {
  7.     R->HighPart = A.HighPart - B.HighPart;
  8.     if (A.LowPart >= B.LowPart)
  9.         R->LowPart = A.LowPart - B.LowPart;
  10.     else
  11.     {
  12.         R->LowPart = A.LowPart - B.LowPart;
  13.         R->HighPart --;
  14.     }
  15. }
  16. #ifdef _M_X64
  17. // see http://msdn2.microsoft.com/library/twchhe95(en-us,vs.80).aspx for __rdtsc
  18. unsigned __int64 __rdtsc(void);
  19. void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
  20. {
  21.  //   printf("rdtsc = %I64xn",__rdtsc());
  22.    pbeginTime64->QuadPart=__rdtsc();
  23. }
  24. LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
  25. {
  26.     LARGE_INTEGER LIres;
  27.     unsigned _int64 res=__rdtsc()-((unsigned _int64)(beginTime64.QuadPart));
  28.     LIres.QuadPart=res;
  29.    // printf("rdtsc = %I64xn",__rdtsc());
  30.     return LIres;
  31. }
  32. #else
  33. #ifdef _M_IX86
  34. void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
  35. {
  36.     DWORD dwEdx,dwEax;
  37.     _asm
  38.     {
  39.         rdtsc
  40.         mov dwEax,eax
  41.         mov dwEdx,edx
  42.     }
  43.     pbeginTime64->LowPart=dwEax;
  44.     pbeginTime64->HighPart=dwEdx;
  45. }
  46. void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
  47. {
  48.     myGetRDTSC32(pbeginTime64);
  49. }
  50. LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
  51. {
  52.     LARGE_INTEGER LIres,endTime64;
  53.     myGetRDTSC32(&endTime64);
  54.     LIres.LowPart=LIres.HighPart=0;
  55.     MyDoMinus64(&LIres,endTime64,beginTime64);
  56.     return LIres;
  57. }
  58. #else
  59. void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
  60. {
  61. }
  62. void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
  63. {
  64. }
  65. LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
  66. {
  67.     LARGE_INTEGER lr;
  68.     lr.QuadPart=0;
  69.     return lr;
  70. }
  71. #endif
  72. #endif
  73. void BeginCountPerfCounter(LARGE_INTEGER * pbeginTime64,BOOL fComputeTimeQueryPerf)
  74. {
  75.     if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(pbeginTime64)))
  76.     {
  77.         pbeginTime64->LowPart = GetTickCount();
  78.         pbeginTime64->HighPart = 0;
  79.     }
  80. }
  81. DWORD GetMsecSincePerfCounter(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
  82. {
  83.     LARGE_INTEGER endTime64,ticksPerSecond,ticks;
  84.     DWORDLONG ticksShifted,tickSecShifted;
  85.     DWORD dwLog=16+0;
  86.     DWORD dwRet;
  87.     if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))
  88.         dwRet = (GetTickCount() - beginTime64.LowPart)*1;
  89.     else
  90.     {
  91.         MyDoMinus64(&ticks,endTime64,beginTime64);
  92.         QueryPerformanceFrequency(&ticksPerSecond);
  93.     
  94.         {
  95.             ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);
  96.             tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);
  97.         
  98.         } 
  99.         dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));
  100.         dwRet *=1;
  101.     }
  102.     return dwRet;
  103. }
  104. int ReadFileMemory(const char* filename,long* plFileSize,void** pFilePtr)
  105. {
  106.     FILE* stream;
  107.     void* ptr;
  108.     int retVal=1;
  109.     stream=fopen(filename, "rb");
  110.     if (stream==NULL)
  111.         return 0;
  112.     fseek(stream,0,SEEK_END);
  113.     *plFileSize=ftell(stream);
  114.     fseek(stream,0,SEEK_SET);
  115.     ptr=malloc((*plFileSize)+1);
  116.     if (ptr==NULL)
  117.         retVal=0;
  118.     else
  119.     {
  120.         if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
  121.             retVal=0;
  122.     }
  123.     fclose(stream);
  124.     *pFilePtr=ptr;
  125.     return retVal;
  126. }
  127. int main(int argc, char *argv[])
  128. {
  129.     int BlockSizeCompress=0x8000;
  130.     int BlockSizeUncompress=0x8000;
  131.     int cprLevel=Z_DEFAULT_COMPRESSION ;
  132.     long lFileSize;
  133.     unsigned char* FilePtr;
  134.     long lBufferSizeCpr;
  135.     long lBufferSizeUncpr;
  136.     long lCompressedSize=0;
  137.     unsigned char* CprPtr;
  138.     unsigned char* UncprPtr;
  139.     long lSizeCpr,lSizeUncpr;
  140.     DWORD dwGetTick,dwMsecQP;
  141.     LARGE_INTEGER li_qp,li_rdtsc,dwResRdtsc;
  142.     if (argc<=1)
  143.     {
  144.         printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]n");
  145.         return 0;
  146.     }
  147.     if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
  148.     {
  149.         printf("error reading %sn",argv[1]);
  150.         return 1;
  151.     }
  152.     else printf("file %s read, %u bytesn",argv[1],lFileSize);
  153.     if (argc>=3)
  154.         BlockSizeCompress=atol(argv[2]);
  155.     if (argc>=4)
  156.         BlockSizeUncompress=atol(argv[3]);
  157.     if (argc>=5)
  158.         cprLevel=(int)atol(argv[4]);
  159.     lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
  160.     lBufferSizeUncpr = lBufferSizeCpr;
  161.     CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
  162.     BeginCountPerfCounter(&li_qp,TRUE);
  163.     dwGetTick=GetTickCount();
  164.     BeginCountRdtsc(&li_rdtsc);
  165.     {
  166.         z_stream zcpr;
  167.         int ret=Z_OK;
  168.         long lOrigToDo = lFileSize;
  169.         long lOrigDone = 0;
  170.         int step=0;
  171.         memset(&zcpr,0,sizeof(z_stream));
  172.         deflateInit(&zcpr,cprLevel);
  173.         zcpr.next_in = FilePtr;
  174.         zcpr.next_out = CprPtr;
  175.         do
  176.         {
  177.             long all_read_before = zcpr.total_in;
  178.             zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
  179.             zcpr.avail_out = BlockSizeCompress;
  180.             ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
  181.             lOrigDone += (zcpr.total_in-all_read_before);
  182.             lOrigToDo -= (zcpr.total_in-all_read_before);
  183.             step++;
  184.         } while (ret==Z_OK);
  185.         lSizeCpr=zcpr.total_out;
  186.         deflateEnd(&zcpr);
  187.         dwGetTick=GetTickCount()-dwGetTick;
  188.         dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
  189.         dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
  190.         printf("total compress size = %u, in %u stepn",lSizeCpr,step);
  191.         printf("time = %u msec = %f secn",dwGetTick,dwGetTick/(double)1000.);
  192.         printf("defcpr time QP = %u msec = %f secn",dwMsecQP,dwMsecQP/(double)1000.);
  193.         printf("defcpr result rdtsc = %I64xnn",dwResRdtsc.QuadPart);
  194.     }
  195.     CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);
  196.     UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
  197.     BeginCountPerfCounter(&li_qp,TRUE);
  198.     dwGetTick=GetTickCount();
  199.     BeginCountRdtsc(&li_rdtsc);
  200.     {
  201.         z_stream zcpr;
  202.         int ret=Z_OK;
  203.         long lOrigToDo = lSizeCpr;
  204.         long lOrigDone = 0;
  205.         int step=0;
  206.         memset(&zcpr,0,sizeof(z_stream));
  207.         inflateInit(&zcpr);
  208.         zcpr.next_in = CprPtr;
  209.         zcpr.next_out = UncprPtr;
  210.         do
  211.         {
  212.             long all_read_before = zcpr.total_in;
  213.             zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
  214.             zcpr.avail_out = BlockSizeUncompress;
  215.             ret=inflate(&zcpr,Z_SYNC_FLUSH);
  216.             lOrigDone += (zcpr.total_in-all_read_before);
  217.             lOrigToDo -= (zcpr.total_in-all_read_before);
  218.             step++;
  219.         } while (ret==Z_OK);
  220.         lSizeUncpr=zcpr.total_out;
  221.         inflateEnd(&zcpr);
  222.         dwGetTick=GetTickCount()-dwGetTick;
  223.         dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
  224.         dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
  225.         printf("total uncompress size = %u, in %u stepn",lSizeUncpr,step);
  226.         printf("time = %u msec = %f secn",dwGetTick,dwGetTick/(double)1000.);
  227.         printf("uncpr  time QP = %u msec = %f secn",dwMsecQP,dwMsecQP/(double)1000.);
  228.         printf("uncpr  result rdtsc = %I64xnn",dwResRdtsc.QuadPart);
  229.     }
  230.     if (lSizeUncpr==lFileSize)
  231.     {
  232.         if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
  233.             printf("compare okn");
  234.     }
  235.     return 0;
  236. }