Skip to content

Instantly share code, notes, and snippets.

@osa1
Created October 26, 2016 12:44
Show Gist options
  • Select an option

  • Save osa1/db79a5b4e22e30f68664b5567b624b52 to your computer and use it in GitHub Desktop.

Select an option

Save osa1/db79a5b4e22e30f68664b5567b624b52 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
typedef struct CursorCursorProd_struct {
char *field0;
char *field1;
} CursorCursorProd;
CursorCursorProd add1(char *t, char *tout)
{
int ttag = *t;
char *t2 = t + 1;
if (ttag == 0) {
*tout = 0;
char *tout2 = tout + 1;
int n = *(int *) t2;
char *t3 = t2 + sizeof(int);
int n1 = n + 1;
*(int *) tout2 = n1;
char *tout3 = tout2 + sizeof(int);
return (CursorCursorProd) {t3, tout3};
} else {
*tout = 1;
char *tout2 = tout + 1;
CursorCursorProd tmp1 = add1(t2, tout2);
char *t3 = tmp1.field0;
char *tout3 = tmp1.field1;
return add1(t3, tout3);
}
}
char *build_tree(int n, char *tout)
{
if (n == 0) {
*(int *) tout = 0;
char *tout1 = tout + sizeof(int);
return tout1;
} else {
int n1 = n - 1;
char *tout1 = build_tree(n1, tout);
char *tout2 = build_tree(n1, tout1);
return tout2;
}
}
void show_usage()
{
// TODO
}
void run(int num_iterations, int tree_size, int buffer_size)
{
printf("Generating initial tree...\n");
char* initial_buffer = (char*)malloc(buffer_size);
assert(initial_buffer);
build_tree(tree_size, initial_buffer);
printf("Benchmarking. Iteration count: %d\n", num_iterations);
char* bench_buffer = (char*)malloc(buffer_size);
assert(bench_buffer);
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
for (int i = 0; i < num_iterations; ++i)
{
add1(initial_buffer, bench_buffer);
}
gettimeofday(&tv2, NULL);
printf("%f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
}
int main(int argc, char** argv)
{
// parameters to parse:
//
// num iterations: How many times to repeat a benchmark. Default: 10.
// tree size: An integer passes to `build_tree()`. Default: 10.
// buffer size: Default 10M.
int num_iterations = 10;
int tree_size = 10;
int buffer_size = 10 * 1000 * 1000; // 10M
// TODO: atoi() error checking
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-num-iterations") == 0 && i < argc - 1)
{
num_iterations = atoi(argv[i + 1]);
++i;
}
else if (strcmp(argv[i], "-tree-size") == 0 && i < argc - 1)
{
tree_size = atoi(argv[i + 1]);
++i;
}
else if (strcmp(argv[i], "-buffer-size") == 0 && i < argc - 1)
{
buffer_size = atoi(argv[i + 1]);
++i;
}
else
{
fprintf(stderr, "Can't parse argument: \"%s\"\n", argv[i]);
show_usage();
exit(1);
}
}
run(num_iterations, tree_size, buffer_size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment