Skip to content

Instantly share code, notes, and snippets.

@guilledk
Created May 13, 2020 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilledk/82f652a3990da96ba0aa34c298520ae3 to your computer and use it in GitHub Desktop.
Save guilledk/82f652a3990da96ba0aa34c298520ae3 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#include "butiac.h"
#include "list.h"
READS = 10000;
int main(int argc, char **argv) {
// butia c init
libusb_context *context = NULL;
CHECK_LIBUSB_RETURNED(libusb_init(&context));
board *robot;
int ret = butiac_init_singleboard(&robot);
DEBUG_PRINT_D("Board init: ", ret)
CHECK_LIBUSB_RETURNED(ret)
module dist = { robot, distanc, 1, 0 };
list *times = lnew();
#ifdef __MACH__ // OS X does not have clock_gettime, use clock_get_time
mach_timespec_t test_start;
mach_timespec_t test_end;
mach_timespec_t before;
mach_timespec_t after;
clock_serv_t cclock;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &test_start);
#else
struct timespec test_start;
struct timespec test_end;
struct timespec before;
struct timespec after;
clock_gettime(CLOCK_MONOTONIC, &test_start);
#endif
for(int i = 0; i < READS; i++){
#ifdef __MACH__
clock_get_time(cclock, &before);
#else
clock_gettime(CLOCK_MONOTONIC, &before);
#endif
int val = mod_getvalue(&dist);
#ifdef __MACH__
clock_get_time(cclock, &after);
#else
clock_gettime(CLOCK_MONOTONIC, &after);
#endif
double *value = malloc(sizeof(double));
*value = ((double)after.tv_sec + 1.0e-9*after.tv_nsec) -
((double)before.tv_sec + 1.0e-9*before.tv_nsec);
ladd(times, value);
printf("read num %d: %d\n", i, val);
}
#ifdef __MACH__
clock_get_time(cclock, &test_end);
#else
clock_gettime(CLOCK_MONOTONIC, &test_end);
#endif
mach_port_deallocate(mach_task_self(), cclock);
double sum = 0;
node *iter = times->head;
for(int i = 0; i < times->size; i++) {
sum += *(double*)(iter->value);
iter = iter->next;
}
double total_seconds =
((double)test_end.tv_sec + 1.0e-9*test_end.tv_nsec) -
((double)test_start.tv_sec + 1.0e-9*test_start.tv_nsec);
printf("total reads: %u\n", READS);
printf("avg sensor read time: %f\n", (sum / READS));
printf("total test time: %f\n", total_seconds);
board_close(robot);
libusb_exit(context);
return 0;
}
from time import time
from usb4butia import USB4Butia
rb = USB4Butia()
times = []
READS = 10000
test_start = time()
for x in range(READS):
before = time()
val = rb.getDistance(1)
after = time()
print("read num " + str(x) + ": " + str(val))
times.append(after - before)
test_end = time()
print("total reads: " + str(READS))
print("avg sensor read time: " + str(sum(times)/READS))
print("total test time: " + str(test_end - test_start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment