diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-13 18:17:56 -0400 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2025-03-14 15:57:00 -0400 |
| commit | 7d93b9e5a2a92f366719a9c471c6bf926915e5c0 (patch) | |
| tree | c918b18504e538da7c151116b7e9e8501dcd5b59 | |
| parent | 64b319d5cc80c1bee2bb18693fa25b03e872b6de (diff) | |
pktmr: BREAKING more denominations
| -rw-r--r-- | pk.h.in | 9 | ||||
| -rw-r--r-- | pktmr.h | 16 | ||||
| -rw-r--r-- | test/pktmr.c | 7 | ||||
| -rw-r--r-- | test/pktmr.cpp | 7 |
4 files changed, 30 insertions, 9 deletions
@@ -175,6 +175,15 @@ * * Offers a set of `pk_tmr*` macros for elapsed time checking. * +* The following definitions (shown with defaults) can be overridden: +* PK_TMR_CLOCK CLOCK_MONOTONIC +* +* If your needs require you to use more than one clock, I recommend calling +* `clock_gettime` manually instead of calling `pk_tmr_start`/`pk_tmr_stop`. +* `pk_tmr.b` is the start time. +* `pk_tmr.e` end the end time. +* You could then call the `pk_tmr_duration...` convenience macros as needed. +* *******************************************************************************/ #define PK_VERSION "@@PK_VERSION@@" @@ -10,7 +10,6 @@ * CLOCK_PROCESS_CPUTIME_ID consistently elapsed thousands of nanoseconds, * even with no work between sequential _start() and _stop() calls. * Meanwhile, the same test with _MONOTONIC elapsed only tens of nanoseconds. - * Consider replacing explicit usage with a define for more user control. */ /* struct pk_tmr */ @@ -19,9 +18,16 @@ struct pk_tmr { struct timespec e; // end }; -#define pk_tmr_start(tmr) { clock_gettime(CLOCK_MONOTONIC, &tmr.b); } -#define pk_tmr_stop(tmr) { clock_gettime(CLOCK_MONOTONIC, &tmr.e); } -#define pk_tmr_duration_double(tmr) ((1000.0 * tmr.e.tv_sec + 1e-6 * tmr.e.tv_nsec) - (1000.0 * tmr.b.tv_sec + 1e-6 * tmr.b.tv_nsec)) -#define pk_tmr_duration_nano(tmr) ((((uint64_t)tmr.e.tv_sec * (uint64_t)1000000000) + tmr.e.tv_nsec) - (((uint64_t)tmr.b.tv_sec * (uint64_t)1000000000) + (uint64_t)tmr.b.tv_nsec)) +#ifndef PK_TMR_CLOCK + #define PK_TMR_CLOCK CLOCK_MONOTONIC +#endif + +#define pk_tmr_start(tmr) { clock_gettime(PK_TMR_CLOCK, &tmr.b); } +#define pk_tmr_stop(tmr) { clock_gettime(PK_TMR_CLOCK, &tmr.e); } +#define pk_tmr_duration_u64_nano(tmr) ((((unsigned long long int)tmr.e.tv_sec * 1000000000llu) + tmr.e.tv_nsec) - (((unsigned long long int)tmr.b.tv_sec * 1000000000llu) + (unsigned long long int)tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_nano(tmr) ((1e+9 * tmr.e.tv_sec + tmr.e.tv_nsec) - (1e+9 * tmr.b.tv_sec + tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_micro(tmr) ((1e+6 * tmr.e.tv_sec + 1e-3 * tmr.e.tv_nsec) - (1e+6 * tmr.b.tv_sec + 1e-3 * tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_mili(tmr) ((1e+3 * tmr.e.tv_sec + 1e-6 * tmr.e.tv_nsec) - (1e+3 * tmr.b.tv_sec + 1e-6 * tmr.b.tv_nsec)) +#define pk_tmr_duration_dbl_scnd(tmr) ((tmr.e.tv_sec + 1e-9 * tmr.e.tv_nsec) - (tmr.b.tv_sec + 1e-9 * tmr.b.tv_nsec)) #endif /* PK_PKTMR_H */ diff --git a/test/pktmr.c b/test/pktmr.c index 8708891..7a62a00 100644 --- a/test/pktmr.c +++ b/test/pktmr.c @@ -18,8 +18,11 @@ int main(int argc, char *argv[]) { pk_tmr_start(tmr); pk_tmr_stop(tmr); - PK_LOGV_INF("%s: elapsed time (nano): %li\n", __FILE__, pk_tmr_duration_nano(tmr)); - PK_LOGV_INF("%s: elapsed time (double): %.9f\n", __FILE__, pk_tmr_duration_double(tmr)); + PK_LOGV_INF("%s: elapsed time (u64) ns: %llu\n", __FILE__, pk_tmr_duration_u64_nano(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) ns: %.9f\n", __FILE__, pk_tmr_duration_dbl_nano(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) μs: %.9f\n", __FILE__, pk_tmr_duration_dbl_micro(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) ms: %.9f\n", __FILE__, pk_tmr_duration_dbl_mili(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) s: %.9f\n", __FILE__, pk_tmr_duration_dbl_scnd(tmr)); } return 0; diff --git a/test/pktmr.cpp b/test/pktmr.cpp index 899b421..0ad1910 100644 --- a/test/pktmr.cpp +++ b/test/pktmr.cpp @@ -18,8 +18,11 @@ int main(int argc, char *argv[]) { pk_tmr_start(tmr); pk_tmr_stop(tmr); - PK_LOGV_INF("%s: elapsed time (nano): %li\n", __FILE__, pk_tmr_duration_nano(tmr)); - PK_LOGV_INF("%s: elapsed time (double): %.9f\n", __FILE__, pk_tmr_duration_double(tmr)); + PK_LOGV_INF("%s: elapsed time (u64) ns: %llu\n", __FILE__, pk_tmr_duration_u64_nano(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) ns: %.9f\n", __FILE__, pk_tmr_duration_dbl_nano(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) μs: %.9f\n", __FILE__, pk_tmr_duration_dbl_micro(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) ms: %.9f\n", __FILE__, pk_tmr_duration_dbl_mili(tmr)); + PK_LOGV_INF("%s: elapsed time (dbl) s: %.9f\n", __FILE__, pk_tmr_duration_dbl_scnd(tmr)); } return 0; |
