Added some timing functionality for convenience

This commit is contained in:
2021-07-31 17:50:03 -04:00
parent 65d3a76bff
commit bb430864cb
4 changed files with 70 additions and 24 deletions

View File

@@ -6,6 +6,10 @@
#include <EmberLib/IO/EmberFile.h>
#include <stdio.h>
/* TEMPORARY */
#include <stdlib.h>
#include <time.h>
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 <EmberLib/Util/EmberTimer.h>
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);
}
}

View File

@@ -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

View File

@@ -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})

27
src/Util/EmberTimer.c Normal file
View File

@@ -0,0 +1,27 @@
/*
* Created by Aaron Helton on 7/31/21.
*/
#include <EmberLib/Util/EmberTimer.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
#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
}