diff --git a/src/base/optime.h b/src/base/optime.h index f1b1c48..be45fe4 100644 --- a/src/base/optime.h +++ b/src/base/optime.h @@ -2,24 +2,40 @@ #include "std.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;\ - kprintf("\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;\ - kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ - }) +#define __optime_print(opname, t)\ + char tnames[3][3]={"s\0","ms","us"};\ + int tni=0;\ + if(t>1000000){\ + t/=1000000;\ + tni=0;\ + } else if(t>1000){\ + t/=1000;\ + tni=1;\ + } else tni=2;\ + kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n",\ + opname, t, tnames[tni]); + +#ifdef CLOCK_REALTIME +/// executes codeblock and prints execution time +/// uint64 op_i is counter of the internal loop +/// uses non-standard high-precision clock +#define optime(opname,repeats,codeblock) ({\ + struct timespec start, stop;\ + clock_gettime(CLOCK_REALTIME, &start);\ + for(uint64 op_i=0;op_i<(uint64)repeats;op_i++)\ + (codeblock);\ + clock_gettime(CLOCK_REALTIME, &stop);\ + double t=(double)(stop.tv_sec-start.tv_sec)*1000000+(double)(stop.tv_nsec-start.tv_nsec)/1000;\ + __optime_print(opname,t)\ +}) +#else +/// uses standard low precision clock +#define optime(opname,repeats,codeblock) ({\ + clock_t start=clock();\ + for(uint64 op_i=0;op_i<(uint64)repeats;op_i++)\ + (codeblock);\ + clock_t stop=clock();\ + double t=(double)(stop-start)/CLOCKS_PER_SEC*1000000;\ + __optime_print(opname,t)\ +}) #endif diff --git a/tests/main.cpp b/tests/main.cpp index e816770..9c43add 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -6,8 +6,7 @@ int main(){ ktDescriptors_beginInit(); ktDescriptors_initKerepTypes(); ktDescriptors_endInit(); - kprintf("\e[97mkerep tests are starting!\n"); - optime("test_all",1,test_all()); + test_all(); ktDescriptors_free(); kprintf("\e[0m\n"); return 0; diff --git a/tests/test_autoarr-vs-vector.cpp b/tests/test_autoarr-vs-vector.cpp index f349435..1ea747d 100644 --- a/tests/test_autoarr-vs-vector.cpp +++ b/tests/test_autoarr-vs-vector.cpp @@ -7,34 +7,28 @@ int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){ kprintf("\e[94mblock count: %u block length: %u count: " IFWIN("%llu", "%lu") "\n", blockCount, blockLength, (uint64)count); Autoarr_int64* ar=Autoarr_create(int64, blockCount, blockLength); std::vector vec=std::vector(); - optime("Autoarr_add", 1, ({ - for(uint32 i=0; i< count; i++) - Autoarr_add(ar, i); - })); - optime("vector_push_back", 1, ({ - for(uint32 i=0; i< count; i++) - vec.push_back(i); - })); + optime("Autoarr_add", count, + Autoarr_add(ar, op_i)); + optime("vector_push_back", count, + vec.push_back(op_i)); int64 t=0; - optime("Autoarr_get", 1, ({ - for(uint32 i=0; i< count; i++) - t=Autoarr_get(ar, i); - })); - optime("vector_get", 1, ({ - for(uint32 i=0; i< count; i++) - t=vec[i]; - })); + optime("Autoarr_get", count, + t=Autoarr_get(ar, op_i)); + optime("vector_get", count, + t=vec[op_i]); Autoarr_free(ar, true); return t; } void test_autoarrVsVector(){ - kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n"); - _autoarrVsVector(4, 16); - _autoarrVsVector(16, 64); - _autoarrVsVector(32, 32); - _autoarrVsVector(64, 64); - _autoarrVsVector(32, 1024); - _autoarrVsVector(256, 256); - _autoarrVsVector(1024, 1024); + optime(__func__, 1, ({ + kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n"); + _autoarrVsVector(4, 16); + _autoarrVsVector(16, 64); + _autoarrVsVector(32, 32); + _autoarrVsVector(64, 64); + _autoarrVsVector(32, 1024); + _autoarrVsVector(256, 256); + _autoarrVsVector(1024, 1024); + })); } diff --git a/tests/tests.h b/tests/tests.h index 780a038..c65a4ba 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -20,19 +20,22 @@ void test_kprint(); void test_type_system(); inline void test_all(){ - test_type_system(); - test_string(); - test_safethrow(); - test_searchtree(); - test_autoarr(); - test_autoarrVsVector(); - test_hash_functions(); - test_hashtable(); - test_dtsod(); - test_rng_algorithms(); - test_kprint_colors(); - test_kprint(); - kprintf("\e[96m--------------------------------------\e[0m\n"); + kprintf("\e[97mkerep tests are starting!\n"); + optime(__func__, 1, ({ + test_type_system(); + test_string(); + test_safethrow(); + test_searchtree(); + test_autoarr(); + test_autoarrVsVector(); + test_hash_functions(); + test_hashtable(); + test_dtsod(); + test_rng_algorithms(); + test_kprint_colors(); + test_kprint(); + kprintf("\e[96m--------------------------------------\e[0m\n"); + })); } #define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T))