From bb430864cb61f5bdb7b3f2a3adef12bf5119110b Mon Sep 17 00:00:00 2001 From: Aaron Helton Date: Sat, 31 Jul 2021 17:50:03 -0400 Subject: [PATCH] Added some timing functionality for convenience --- demo/demo.c | 48 ++++++++++++++++-------------- include/EmberLib/Util/EmberTimer.h | 10 +++++++ src/CMakeLists.txt | 9 ++++-- src/Util/EmberTimer.c | 27 +++++++++++++++++ 4 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 include/EmberLib/Util/EmberTimer.h create mode 100644 src/Util/EmberTimer.c diff --git a/demo/demo.c b/demo/demo.c index 4a89ad1..895805c 100644 --- a/demo/demo.c +++ b/demo/demo.c @@ -6,6 +6,10 @@ #include #include +/* TEMPORARY */ +#include +#include + void print_bitarray(BitArray *array) { for(uint32 i = 0; i < ebitarray_size(array); i++) @@ -15,32 +19,32 @@ void print_bitarray(BitArray *array) printf("\n"); } +#include int main(void) { - BitArray *array = ebitarray_create(32); - printf("Current Array: "); - print_bitarray(array); + for(int i = 0; i < 3; i++) + { + srand(771996); - ebitarray_set(array, 1, 1); - printf("After setting bit pos 1 to 1: "); - print_bitarray(array); + double start = get_time(); + for(int i = 0; i < 10000000; i++) + { + BitArray *array = ebitarray_create(1024); - ebitarray_set(array, 1, 0); - printf("After setting bit pos 1 to 0: "); - print_bitarray(array); + // set a random bit some random number of times + int times = 100; + for(int j = 0; j < times; j++) + { + uint32 index = rand() % 32; + uint8 value = rand() & 1; + ebitarray_set(array, index, value); + } - ebitarray_set(array, 1, 20); - printf("After setting bit pos 1 to 20: "); - print_bitarray(array); + ebitarray_destroy(array); + array = NULL; + } + double end = get_time(); - ebitarray_set_range(array, 10, 20, 1); - printf("After setting bits 10-20 to 1: "); - print_bitarray(array); - - ebitarray_set_range(array, 10, 20, 0); - printf("After setting bits 10-20 to 0: "); - print_bitarray(array); - - ebitarray_destroy(array); - return 0; + printf("Elapsed Time: %f\n", end - start); + } } diff --git a/include/EmberLib/Util/EmberTimer.h b/include/EmberLib/Util/EmberTimer.h new file mode 100644 index 0000000..7607503 --- /dev/null +++ b/include/EmberLib/Util/EmberTimer.h @@ -0,0 +1,10 @@ +/* + * Created by Aaron Helton on 7/31/21. + */ + +#ifndef EMBERLIB_EMBERTIMER_H +#define EMBERLIB_EMBERTIMER_H + +double get_time(void); + +#endif//EMBERLIB_EMBERTIMER_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bc3962f..451d6e6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,13 +1,18 @@ set(header_path ${CMAKE_CURRENT_SOURCE_DIR}/../include/EmberLib) + set(headers ${CONFIG_HEADER} ${header_path}/IO/EmberFile.h ${header_path}/Util/EmberString.h ${header_path}/Util/EmberList.h - ${header_path}/Util/EmberDefines.h) -set(sources Util/EmberList.c + ${header_path}/Util/EmberDefines.h + ${header_path}/Util/EmberTimer.h) + +set(sources + Util/EmberList.c Util/EmberString.c Util/EmberMath.c Util/BitArray.c + Util/EmberTimer.c IO/EmberFile.c) add_library(EmberLib STATIC ${headers} ${sources}) diff --git a/src/Util/EmberTimer.c b/src/Util/EmberTimer.c new file mode 100644 index 0000000..f22d52d --- /dev/null +++ b/src/Util/EmberTimer.c @@ -0,0 +1,27 @@ +/* + * Created by Aaron Helton on 7/31/21. + */ + +#include + +#ifdef WIN32 + #include +#else + #include + #include +#endif + +double get_time() +{ +#ifdef WIN32 + LARGE_INTEGER t, f; + QueryPerformanceCounter(&t); + QueryPerformanceFrequency(&f); + return (double)t.QuadPart/(double)f.QuadPart; +#else + struct timeval t; + struct timezone tzp; + gettimeofday(&t, &tzp); + return (double)t.tv_sec + t.tv_usec*1e-6; +#endif +} \ No newline at end of file