testzlib.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 = %I64x\n",__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 = %I64x\n",__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. ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);
  95. tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);
  96. }
  97. dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));
  98. dwRet *=1;
  99. }
  100. return dwRet;
  101. }
  102. int ReadFileMemory(const char* filename,long* plFileSize,void** pFilePtr)
  103. {
  104. FILE* stream;
  105. void* ptr;
  106. int retVal=1;
  107. stream=fopen(filename, "rb");
  108. if (stream==NULL)
  109. return 0;
  110. fseek(stream,0,SEEK_END);
  111. *plFileSize=ftell(stream);
  112. fseek(stream,0,SEEK_SET);
  113. ptr=malloc((*plFileSize)+1);
  114. if (ptr==NULL)
  115. retVal=0;
  116. else
  117. {
  118. if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
  119. retVal=0;
  120. }
  121. fclose(stream);
  122. *pFilePtr=ptr;
  123. return retVal;
  124. }
  125. int main(int argc, char *argv[])
  126. {
  127. int BlockSizeCompress=0x8000;
  128. int BlockSizeUncompress=0x8000;
  129. int cprLevel=Z_DEFAULT_COMPRESSION ;
  130. long lFileSize;
  131. unsigned char* FilePtr;
  132. long lBufferSizeCpr;
  133. long lBufferSizeUncpr;
  134. long lCompressedSize=0;
  135. unsigned char* CprPtr;
  136. unsigned char* UncprPtr;
  137. long lSizeCpr,lSizeUncpr;
  138. DWORD dwGetTick,dwMsecQP;
  139. LARGE_INTEGER li_qp,li_rdtsc,dwResRdtsc;
  140. if (argc<=1)
  141. {
  142. printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");
  143. return 0;
  144. }
  145. if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
  146. {
  147. printf("error reading %s\n",argv[1]);
  148. return 1;
  149. }
  150. else printf("file %s read, %u bytes\n",argv[1],lFileSize);
  151. if (argc>=3)
  152. BlockSizeCompress=atol(argv[2]);
  153. if (argc>=4)
  154. BlockSizeUncompress=atol(argv[3]);
  155. if (argc>=5)
  156. cprLevel=(int)atol(argv[4]);
  157. lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
  158. lBufferSizeUncpr = lBufferSizeCpr;
  159. CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
  160. BeginCountPerfCounter(&li_qp,TRUE);
  161. dwGetTick=GetTickCount();
  162. BeginCountRdtsc(&li_rdtsc);
  163. {
  164. z_stream zcpr;
  165. int ret=Z_OK;
  166. long lOrigToDo = lFileSize;
  167. long lOrigDone = 0;
  168. int step=0;
  169. memset(&zcpr,0,sizeof(z_stream));
  170. deflateInit(&zcpr,cprLevel);
  171. zcpr.next_in = FilePtr;
  172. zcpr.next_out = CprPtr;
  173. do
  174. {
  175. long all_read_before = zcpr.total_in;
  176. zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
  177. zcpr.avail_out = BlockSizeCompress;
  178. ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
  179. lOrigDone += (zcpr.total_in-all_read_before);
  180. lOrigToDo -= (zcpr.total_in-all_read_before);
  181. step++;
  182. } while (ret==Z_OK);
  183. lSizeCpr=zcpr.total_out;
  184. deflateEnd(&zcpr);
  185. dwGetTick=GetTickCount()-dwGetTick;
  186. dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
  187. dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
  188. printf("total compress size = %u, in %u step\n",lSizeCpr,step);
  189. printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
  190. printf("defcpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
  191. printf("defcpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
  192. }
  193. CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);
  194. UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
  195. BeginCountPerfCounter(&li_qp,TRUE);
  196. dwGetTick=GetTickCount();
  197. BeginCountRdtsc(&li_rdtsc);
  198. {
  199. z_stream zcpr;
  200. int ret=Z_OK;
  201. long lOrigToDo = lSizeCpr;
  202. long lOrigDone = 0;
  203. int step=0;
  204. memset(&zcpr,0,sizeof(z_stream));
  205. inflateInit(&zcpr);
  206. zcpr.next_in = CprPtr;
  207. zcpr.next_out = UncprPtr;
  208. do
  209. {
  210. long all_read_before = zcpr.total_in;
  211. zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
  212. zcpr.avail_out = BlockSizeUncompress;
  213. ret=inflate(&zcpr,Z_SYNC_FLUSH);
  214. lOrigDone += (zcpr.total_in-all_read_before);
  215. lOrigToDo -= (zcpr.total_in-all_read_before);
  216. step++;
  217. } while (ret==Z_OK);
  218. lSizeUncpr=zcpr.total_out;
  219. inflateEnd(&zcpr);
  220. dwGetTick=GetTickCount()-dwGetTick;
  221. dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
  222. dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
  223. printf("total uncompress size = %u, in %u step\n",lSizeUncpr,step);
  224. printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
  225. printf("uncpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
  226. printf("uncpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
  227. }
  228. if (lSizeUncpr==lFileSize)
  229. {
  230. if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
  231. printf("compare ok\n");
  232. }
  233. return 0;
  234. }