From 8c005321c2f467e1fc3e481587d3d25c6ed336f1 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 2 Jun 2022 20:44:16 +0300 Subject: [PATCH] optime.h --- src/base/base.h | 23 +---------------------- src/base/optime.h | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 22 deletions(-) create mode 100644 src/base/optime.h diff --git a/src/base/base.h b/src/base/base.h index f9d56b8..d49b6d7 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -8,28 +8,7 @@ extern "C" { #include "types.h" #include "errors.h" #include "cptr.h" - -// executes codeblock and prints execution time -#ifdef CLOCK_REALTIME // non-standard high-precision clock - #define optime(opname,repeats,codeblock) ({\ - struct timespec start, stop;\ - clock_gettime(CLOCK_REALTIME, &start);\ - for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ - (codeblock);\ - clock_gettime(CLOCK_REALTIME, &stop);\ - double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ - printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ - }) -#else // - #define optime(opname,repeats,codeblock) ({\ - clock_t start=clock();\ - for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ - (codeblock);\ - clock_t stop=clock();\ - double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\ - printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ - }) -#endif +#include "optime.h" #if __cplusplus } diff --git a/src/base/optime.h b/src/base/optime.h new file mode 100644 index 0000000..ea7446c --- /dev/null +++ b/src/base/optime.h @@ -0,0 +1,25 @@ +#pragma once + +#include "types.h" + +// executes codeblock and prints execution time +#ifdef CLOCK_REALTIME // non-standard high-precision clock + #define optime(opname,repeats,codeblock) ({\ + struct timespec start, stop;\ + clock_gettime(CLOCK_REALTIME, &start);\ + for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ + (codeblock);\ + clock_gettime(CLOCK_REALTIME, &stop);\ + double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ + printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ + }) +#else // standard low precision clock + #define optime(opname,repeats,codeblock) ({\ + clock_t start=clock();\ + for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ + (codeblock);\ + clock_t stop=clock();\ + double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\ + printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ + }) +#endif