minor simplification refactoring for timefn
`UTIL_getSpanTimeMicro()` can be factored in a generic way,
reducing OS-dependent code.
diff --git a/programs/fileio.c b/programs/fileio.c
index 6bfa559..1e34866 100644
--- a/programs/fileio.c
+++ b/programs/fileio.c
@@ -28,6 +28,7 @@
#include <stdio.h> /* fprintf, open, fdopen, fread, _fileno, stdin, stdout */
#include <stdlib.h> /* malloc, free */
#include <string.h> /* strcmp, strlen */
+#include <time.h> /* clock_t, to measure process time */
#include <fcntl.h> /* O_WRONLY */
#include <assert.h>
#include <errno.h> /* errno */
diff --git a/programs/timefn.c b/programs/timefn.c
index 7005b26..08aa1cf 100644
--- a/programs/timefn.c
+++ b/programs/timefn.c
@@ -25,20 +25,6 @@
UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
-PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
-{
- static LARGE_INTEGER ticksPerSecond;
- static int init = 0;
- if (!init) {
- if (!QueryPerformanceFrequency(&ticksPerSecond)) {
- perror("timefn::QueryPerformanceFrequency");
- abort();
- }
- init = 1;
- }
- return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
-}
-
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
{
static LARGE_INTEGER ticksPerSecond;
@@ -59,17 +45,6 @@
UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
-PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
-{
- static mach_timebase_info_data_t rate;
- static int init = 0;
- if (!init) {
- mach_timebase_info(&rate);
- init = 1;
- }
- return (((clockEnd - clockStart) * (PTime)rate.numer) / ((PTime)rate.denom))/1000ULL;
-}
-
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
{
static mach_timebase_info_data_t rate;
@@ -115,15 +90,6 @@
return diff;
}
-PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
-{
- UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
- PTime micro = 0;
- micro += 1000000ULL * diff.tv_sec;
- micro += diff.tv_nsec / 1000ULL;
- return micro;
-}
-
PTime UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
{
UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
@@ -134,25 +100,26 @@
}
-
-#else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
+#else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
UTIL_time_t UTIL_getTime(void) { return clock(); }
-PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
#endif
+/* ==== Common functions, valid for all time API ==== */
+PTime UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
+{
+ return UTIL_getSpanTimeNano(begin, end) / 1000ULL;
+}
-/* returns time span in microseconds */
PTime UTIL_clockSpanMicro(UTIL_time_t clockStart )
{
UTIL_time_t const clockEnd = UTIL_getTime();
return UTIL_getSpanTimeMicro(clockStart, clockEnd);
}
-/* returns time span in microseconds */
PTime UTIL_clockSpanNano(UTIL_time_t clockStart )
{
UTIL_time_t const clockEnd = UTIL_getTime();
diff --git a/programs/timefn.h b/programs/timefn.h
index 438a8de..3e4476a 100644
--- a/programs/timefn.h
+++ b/programs/timefn.h
@@ -16,12 +16,6 @@
#endif
-/*-****************************************
-* Dependencies
-******************************************/
-#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */
-
-
/*-****************************************
* Local Types
@@ -31,7 +25,7 @@
# if defined(_AIX)
# include <inttypes.h>
# else
-# include <stdint.h> /* intptr_t */
+# include <stdint.h> /* uint64_t */
# endif
typedef uint64_t PTime; /* Precise Time */
#else
@@ -41,8 +35,10 @@
/*-****************************************
-* Time functions
+* Time types (note: OS dependent)
******************************************/
+#include <time.h> /* TIME_UTC, then struct timespec and clock_t */
+
#if defined(_WIN32) /* Windows */
#include <windows.h> /* LARGE_INTEGER */
@@ -63,7 +59,7 @@
typedef struct timespec UTIL_time_t;
#define UTIL_TIME_INITIALIZER { 0, 0 }
-#else /* relies on standard C90 (note : clock_t measurements can be wrong when using multi-threading) */
+#else /* relies on standard C90 (note : clock_t produces wrong measurements for multi-threaded workloads) */
#define UTIL_TIME_USES_C90_CLOCK
typedef clock_t UTIL_time_t;
@@ -72,15 +68,21 @@
#endif
-UTIL_time_t UTIL_getTime(void);
-PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
-PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
+/*-****************************************
+* Time functions
+******************************************/
-#define SEC_TO_MICRO ((PTime)1000000)
-PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
+UTIL_time_t UTIL_getTime(void);
+void UTIL_waitForNextTick(void);
+
+PTime UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd);
PTime UTIL_clockSpanNano(UTIL_time_t clockStart);
-void UTIL_waitForNextTick(void);
+#define SEC_TO_MICRO ((PTime)1000000)
+PTime UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd);
+PTime UTIL_clockSpanMicro(UTIL_time_t clockStart);
+
+
#if defined (__cplusplus)
diff --git a/tests/decodecorpus.c b/tests/decodecorpus.c
index 20ad5ad..15e89d5 100644
--- a/tests/decodecorpus.c
+++ b/tests/decodecorpus.c
@@ -14,6 +14,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h> /* time(), for seed random initialization */
#include "util.h"
#include "timefn.h" /* UTIL_clockSpanMicro, SEC_TO_MICRO, UTIL_TIME_INITIALIZER */
@@ -24,7 +25,7 @@
#include "zdict.h"
/* Direct access to internal compression functions is required */
-#include "zstd_compress.c"
+#include "zstd_compress.c" /* ZSTD_resetSeqStore, ZSTD_storeSeq, *_TO_OFFBASE, HIST_countFast_wksp, HIST_isError */
#define XXH_STATIC_LINKING_ONLY
#include "xxhash.h" /* XXH64 */
@@ -165,7 +166,7 @@
/*-*******************************************************
* Constants and Structs
*********************************************************/
-const char *BLOCK_TYPES[] = {"raw", "rle", "compressed"};
+const char* BLOCK_TYPES[] = {"raw", "rle", "compressed"};
#define MAX_DECOMPRESSED_SIZE_LOG 20
#define MAX_DECOMPRESSED_SIZE (1ULL << MAX_DECOMPRESSED_SIZE_LOG)
diff --git a/tests/zstreamtest.c b/tests/zstreamtest.c
index 664ff63..10239d3 100644
--- a/tests/zstreamtest.c
+++ b/tests/zstreamtest.c
@@ -25,6 +25,7 @@
#include <stdlib.h> /* free */
#include <stdio.h> /* fgets, sscanf */
#include <string.h> /* strcmp */
+#include <time.h> /* time_t, time(), to randomize seed */
#include <assert.h> /* assert */
#include "timefn.h" /* UTIL_time_t, UTIL_getTime */
#include "mem.h"