This commit is contained in:
timerix 2023-01-20 01:17:52 +06:00
parent c5e1a42eb7
commit 40d1828f2c
4 changed files with 71 additions and 59 deletions

View File

@ -2,24 +2,40 @@
#include "std.h" #include "std.h"
// executes codeblock and prints execution time #define __optime_print(opname, t)\
#ifdef CLOCK_REALTIME // non-standard high-precision clock char tnames[3][3]={"s\0","ms","us"};\
#define optime(opname,repeats,codeblock) ({\ int tni=0;\
struct timespec start, stop;\ if(t>1000000){\
clock_gettime(CLOCK_REALTIME, &start);\ t/=1000000;\
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ tni=0;\
(codeblock);\ } else if(t>1000){\
clock_gettime(CLOCK_REALTIME, &stop);\ t/=1000;\
double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ tni=1;\
kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ } else tni=2;\
}) kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n",\
#else // standard low precision clock opname, t, tnames[tni]);
#define optime(opname,repeats,codeblock) ({\
clock_t start=clock();\ #ifdef CLOCK_REALTIME
for(uint64 ___OPREP=0;___OPREP<(uint64)repeats;___OPREP++)\ /// executes codeblock and prints execution time
(codeblock);\ /// uint64 op_i is counter of the internal loop
clock_t stop=clock();\ /// uses non-standard high-precision clock
double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\ #define optime(opname,repeats,codeblock) ({\
kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ 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 #endif

View File

@ -6,8 +6,7 @@ int main(){
ktDescriptors_beginInit(); ktDescriptors_beginInit();
ktDescriptors_initKerepTypes(); ktDescriptors_initKerepTypes();
ktDescriptors_endInit(); ktDescriptors_endInit();
kprintf("\e[97mkerep tests are starting!\n"); test_all();
optime("test_all",1,test_all());
ktDescriptors_free(); ktDescriptors_free();
kprintf("\e[0m\n"); kprintf("\e[0m\n");
return 0; return 0;

View File

@ -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); 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); Autoarr_int64* ar=Autoarr_create(int64, blockCount, blockLength);
std::vector<int64> vec=std::vector<int64>(); std::vector<int64> vec=std::vector<int64>();
optime("Autoarr_add", 1, ({ optime("Autoarr_add", count,
for(uint32 i=0; i< count; i++) Autoarr_add(ar, op_i));
Autoarr_add(ar, i); optime("vector_push_back", count,
})); vec.push_back(op_i));
optime("vector_push_back", 1, ({
for(uint32 i=0; i< count; i++)
vec.push_back(i);
}));
int64 t=0; int64 t=0;
optime("Autoarr_get", 1, ({ optime("Autoarr_get", count,
for(uint32 i=0; i< count; i++) t=Autoarr_get(ar, op_i));
t=Autoarr_get(ar, i); optime("vector_get", count,
})); t=vec[op_i]);
optime("vector_get", 1, ({
for(uint32 i=0; i< count; i++)
t=vec[i];
}));
Autoarr_free(ar, true); Autoarr_free(ar, true);
return t; return t;
} }
void test_autoarrVsVector(){ void test_autoarrVsVector(){
kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n"); optime(__func__, 1, ({
_autoarrVsVector(4, 16); kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n");
_autoarrVsVector(16, 64); _autoarrVsVector(4, 16);
_autoarrVsVector(32, 32); _autoarrVsVector(16, 64);
_autoarrVsVector(64, 64); _autoarrVsVector(32, 32);
_autoarrVsVector(32, 1024); _autoarrVsVector(64, 64);
_autoarrVsVector(256, 256); _autoarrVsVector(32, 1024);
_autoarrVsVector(1024, 1024); _autoarrVsVector(256, 256);
_autoarrVsVector(1024, 1024);
}));
} }

View File

@ -20,19 +20,22 @@ void test_kprint();
void test_type_system(); void test_type_system();
inline void test_all(){ inline void test_all(){
test_type_system(); kprintf("\e[97mkerep tests are starting!\n");
test_string(); optime(__func__, 1, ({
test_safethrow(); test_type_system();
test_searchtree(); test_string();
test_autoarr(); test_safethrow();
test_autoarrVsVector(); test_searchtree();
test_hash_functions(); test_autoarr();
test_hashtable(); test_autoarrVsVector();
test_dtsod(); test_hash_functions();
test_rng_algorithms(); test_hashtable();
test_kprint_colors(); test_dtsod();
test_kprint(); test_rng_algorithms();
kprintf("\e[96m--------------------------------------\e[0m\n"); 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)) #define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T))