From 2d7df8b306753bd68a5597327da06c44c8bd80ca Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Wed, 29 Jun 2016 14:35:19 +0200 Subject: Reduced bench dependency to standard C Faster tests More accurate results on systems with high resolution clocks --- programs/bench.c | 107 ++++++++++++++++++++----------------------------------- 1 file changed, 39 insertions(+), 68 deletions(-) diff --git a/programs/bench.c b/programs/bench.c index 1e14b83..d11f487 100644 --- a/programs/bench.c +++ b/programs/bench.c @@ -44,17 +44,11 @@ /*-************************************ * Includes ***************************************/ -#include /* malloc */ -#include /* fprintf, fopen, ftello64 */ -#include /* stat64 */ -#include /* stat64 */ - -/* Use ftime() if gettimeofday() is not available on your target */ -#if defined(BMK_LEGACY_TIMER) -# include /* timeb, ftime */ -#else -# include /* gettimeofday */ -#endif +#include /* malloc */ +#include /* fprintf, fopen */ +#include /* stat64 */ +#include /* stat64 */ +#include /* clock_t, clock, CLOCKS_PER_SEC */ #include "lz4.h" #define COMPRESSOR0 LZ4_compress_local @@ -97,7 +91,8 @@ static int LZ4_compress_local(const char* src, char* dst, int srcSize, int dstSi * Constants ***************************************/ #define NBLOOPS 3 -#define TIMELOOP 2000 +#define TIMELOOP_S 1 +#define TIMELOOP_CLOCK (TIMELOOP_S * CLOCKS_PER_SEC) #define KB *(1 <<10) #define MB *(1 <<20) @@ -154,42 +149,11 @@ void BMK_setPause(void) { BMK_pause = 1; } * Private functions **********************************************************/ -#if defined(BMK_LEGACY_TIMER) - -static int BMK_GetMilliStart(void) -{ - /* Based on Legacy ftime() - Rolls over every ~ 12.1 days (0x100000/24/60/60) - Use GetMilliSpan to correct for rollover */ - struct timeb tb; - int nCount; - ftime( &tb ); - nCount = (int) (tb.millitm + (tb.time & 0xfffff) * 1000); - return nCount; -} - -#else - -static int BMK_GetMilliStart(void) -{ - /* Based on newer gettimeofday() - Use GetMilliSpan to correct for rollover */ - struct timeval tv; - int nCount; - gettimeofday(&tv, NULL); - nCount = (int) (tv.tv_usec/1000 + (tv.tv_sec & 0xfffff) * 1000); - return nCount; -} - -#endif - - -static int BMK_GetMilliSpan( int nTimeStart ) +/** BMK_getClockSpan() : + works even if overflow; Typical max span ~ 30 mn */ +static clock_t BMK_getClockSpan (clock_t clockStart) { - int nSpan = BMK_GetMilliStart() - nTimeStart; - if ( nSpan < 0 ) - nSpan += 0x100000 * 1000; - return nSpan; + return clock() - clockStart; } @@ -341,7 +305,7 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel) DISPLAY("\r%79s\r", ""); for (loopNb = 1; loopNb <= nbIterations; loopNb++) { int nbLoops; - int milliTime; + clock_t clockStart, clockEnd; unsigned chunkNb; /* Compression */ @@ -349,40 +313,43 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel) { size_t i; for (i=0; i %9i (%5.2f%%),%7.1f MB/s\r", loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000.); + DISPLAY("%1i-%-14.14s : %9i -> %9i (%5.2f%%),%7.1f MB/s\r", + loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / (fastestC / CLOCKS_PER_SEC) / 1000000); /* Decompression */ { size_t i; for (i=0; i %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s \r", loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.); + if ((double)clockEnd < fastestD*nbLoops) fastestD = (double)clockEnd/nbLoops; + DISPLAY("%1i-%-14.14s : %9i -> %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s \r", + loopNb, inFileName, (int)benchedSize, (int)cSize, ratio, + (double)benchedSize / (fastestC / CLOCKS_PER_SEC) / 1000000, (double)benchedSize / (fastestD / CLOCKS_PER_SEC) / 1000000 ); /* CRC Checking */ crcCheck = XXH32(orig_buff, benchedSize,0); @@ -391,9 +358,13 @@ int BMK_benchFiles(const char** fileNamesTable, int nbFiles, int cLevel) if (crcOrig==crcCheck) { if (ratio<100.) - DISPLAY("%-16.16s : %9i -> %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s \n", inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.); + DISPLAY("%-16.16s : %9i -> %9i (%5.2f%%),%7.1f MB/s ,%7.1f MB/s \n", + inFileName, (int)benchedSize, (int)cSize, ratio, + (double)benchedSize / (fastestC / CLOCKS_PER_SEC) / 1000000, (double)benchedSize / (fastestD / CLOCKS_PER_SEC) / 1000000 ); else - DISPLAY("%-16.16s : %9i -> %9i (%5.1f%%),%7.1f MB/s ,%7.1f MB/s \n", inFileName, (int)benchedSize, (int)cSize, ratio, (double)benchedSize / fastestC / 1000., (double)benchedSize / fastestD / 1000.); + DISPLAY("%-16.16s : %9i -> %9i (%5.1f%%),%7.1f MB/s ,%7.1f MB/s \n", + inFileName, (int)benchedSize, (int)cSize, ratio, + (double)benchedSize / (fastestC / CLOCKS_PER_SEC) / 1000000, (double)benchedSize / (fastestD / CLOCKS_PER_SEC) / 1000000 ); } totals += benchedSize; totalz += cSize; -- cgit v0.12