Skip to content

Instantly share code, notes, and snippets.

@jflatow
Last active August 29, 2015 14:06
Show Gist options
  • Save jflatow/2293d6ddd9b2ec418468 to your computer and use it in GitHub Desktop.
Save jflatow/2293d6ddd9b2ec418468 to your computer and use it in GitHub Desktop.
/*
* Primitive benchmarks of buoy vs tree.
* Assuming {buoy,tree}.{c,h} and hash.h, run like:
* cc bench.c buoy.c tree.c -o bench && ./bench 100000 3 10000
*/
#include <stdio.h>
#include <time.h>
#include "buoy.h"
#include "tree.h"
char *timer(Buoy *buoy_a, Buoy *buoy_b, Tree *tree_a, Tree *tree_b, char *buf) {
clock_t start;
long bdelta, tdelta;
double bdot, tdot;
start = clock();
bdot = buoy_dot(buoy_a, buoy_b);
bdelta = clock() - start;
start = clock();
tdot = tree_dot(tree_a, tree_b);
tdelta = clock() - start;
sprintf(buf, "%d\tbdot: %20.1lf\ttdot: %20.1lf\tbclock: %ld\ttclock: %ld", bdot == tdot, bdot, tdot, bdelta, tdelta);
return buf;
}
int main(int argc, char **argv) {
int n = argc > 1 ? atoi(argv[1]) : 1000;
int k = argc > 2 ? atoi(argv[2]) : 3;
int R = argc > 3 ? atoi(argv[3]) : RAND_MAX;
Buoy *buoy_a, *buoy_b;
Tree *tree_a, *tree_b;
char buf[255];
for (int c = 0; c < k; c++) {
buoy_a = buoy_new();
buoy_b = buoy_new();
tree_a = tree_new();
tree_b = tree_new();
for (int i = 0; i < n; i++) {
sprintf(buf, "%d", rand() % R);
Attr *aattr = &(Attr){.bytes = (byte *)buf, .length = strlen(buf)};
buoy_store(buoy_a, aattr, i);
tree_store(tree_a, buf, i);
sprintf(buf, "%d", rand() % R);
Attr *battr = &(Attr){.bytes = (byte *)buf, .length = strlen(buf)};
buoy_store(buoy_b, battr, i);
tree_store(tree_b, buf, i);
}
printf("aa\t%d\t%s\n", n, timer(buoy_a, buoy_a, tree_a, tree_a, buf));
printf("bb\t%d\t%s\n", n, timer(buoy_b, buoy_b, tree_b, tree_b, buf));
printf("ab\t%d\t%s\n", n, timer(buoy_a, buoy_b, tree_a, tree_b, buf));
printf("ba\t%d\t%s\n", n, timer(buoy_b, buoy_a, tree_b, tree_a, buf));
tree_free(tree_a);
tree_free(tree_b);
buoy_free(buoy_a);
buoy_free(buoy_b);
}
return 0;
}
/*
* Something like:
* cc -std=c++11 -Llibcuckoo/.libs bench.cc buoy.o -lcityhash -lstdc++ -o bench && DYLD_LIBRARY_PATH=libcuckoo/.libs ./bench 1000000
*/
#include <stdio.h>
#include <time.h>
#include "libcuckoo/cuckoohash_map.hh"
#include "libcuckoo/city_hasher.hh"
extern "C" {
#include "buoy.h"
}
typedef cuckoohash_map< std::string, double, CityHasher<std::string> > Cuckoo;
// a and b must be distinct, or libcuckoo locks
double cuckoo_dot(Cuckoo *a, Cuckoo *b) {
double prod = 0, val;
for (auto iter = a->begin(); !iter.is_end(); iter++)
if (b->find(iter->first, val))
prod += iter->second * val;
return prod;
}
char *timer(Buoy *buoy_a, Buoy *buoy_b, Cuckoo *cuck_a, Cuckoo *cuck_b, char *buf) {
clock_t start;
long bdelta, cdelta;
double bdot, cdot;
start = clock();
bdot = buoy_dot(buoy_a, buoy_b);
bdelta = clock() - start;
start = clock();
cdot = cuckoo_dot(cuck_a, cuck_b);
cdelta = clock() - start;
sprintf(buf, "%d\tbdot: %20.1lf\tcdot: %20.1lf\tbclock: %ld\tcclock: %ld", bdot == cdot, bdot, cdot, bdelta, cdelta);
return buf;
}
int main(int argc, char **argv) {
int n = argc > 1 ? atoi(argv[1]) : 1000;
int R = argc > 2 ? atoi(argv[2]) : RAND_MAX;
char buf[255];
Buoy *buoy_a = buoy_new(), *buoy_b = buoy_new();
Cuckoo cuck_a, cuck_b;
printf("n: %d R: %d\n", n, R);
for (int i = 0; i < n; i++) {
sprintf(buf, "%d", rand() % R);
Attr aattr = {.bytes = (byte *)buf, .length = (byte)strlen(buf)};
buoy_store(buoy_a, &aattr, i);
cuck_a.insert(std::string(buf), i);
sprintf(buf, "%d", rand() % R);
Attr battr = {.bytes = (byte *)buf, .length = (byte)strlen(buf)};
buoy_store(buoy_b, &battr, i);
cuck_b.insert(std::string(buf), i);
}
printf("ab\t%d\t%s\n", n, timer(buoy_a, buoy_b, &cuck_a, &cuck_b, buf));
printf("ba\t%d\t%s\n", n, timer(buoy_b, buoy_a, &cuck_b, &cuck_a, buf));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment