Skip to content

Commit

Permalink
Working benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
David Koblas committed Mar 17, 2011
1 parent b6df07a commit 7e4f600
Show file tree
Hide file tree
Showing 34 changed files with 1,000,748 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
@@ -0,0 +1,8 @@

DIRS=c++ ruby python perl php

bench:
@for dir in $(DIRS); do \
echo $$dir ; \
$(MAKE) --no-print-directory -C $$dir bench; \
done
11 changes: 11 additions & 0 deletions README
@@ -0,0 +1,11 @@
Array Intersection Bake-off

One of those moments where an interview question turns into a research
project, or is it really a bake off? The simple problem is demonstrate
an algorithm to intersect two lists of numbers, fundamentally it's
question about using modern interpreted languages and their associative
array bits to make a simple intersection routine. However many languages
support many different ways to do things. I've put together a test
of Python vs. Java vs. Ruby vs. Perl vs. PHP and got a few interesting
benchmarks.

8 changes: 8 additions & 0 deletions c++/Makefile
@@ -0,0 +1,8 @@

bench: isect_1_cxx
@echo -n 'isect_1_cxx | '
@./isect_1_cxx ../data.txt

isect_1_cxx : isect_1_cxx.cxx
$(CXX) -g -o $@ isect_1_cxx.cxx

Binary file added c++/isect_1_cxx
Binary file not shown.
55 changes: 55 additions & 0 deletions c++/isect_1_cxx.cxx
@@ -0,0 +1,55 @@
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <backward/hash_set>
#include <list>
#include <time.h>

std::list<int>* isect(int len, int a[], int b[]) {
__gnu_cxx::hash_set<int> h = __gnu_cxx::hash_set<int>();

for (int i = 0; i < len; i++)
h.insert(a[i]);

std::list<int> *l = new std::list<int>();

for (int i = 0; i < len; i++)
if (h.find(b[i]) != h.end())
l->push_back(b[i]);

return l;
}

main(int argc, char *argv[]) {
FILE *fd;
char buf[1024];
int size;

if (argc == 1)
fd = stdin;
else
fd = fopen(argv[1], "r");

fgets(buf, sizeof(buf), fd);
size = atoi(buf);

int *x = new int[size];
int *y = new int[size];
int idx = 0;

while (fgets(buf, sizeof(buf), fd) != NULL) {
const char *cp = index(buf, ' ');

x[idx] = atoi(buf);
y[idx] = atoi(cp+1);
idx++;
}

clock_t t0 = clock();
std::list<int> *l = isect(idx, x, y);
clock_t t1 = clock();

printf("Set | n = %d : %d intersects found in %f seconds\n", idx, l->size(), (double)(t1 - t0) / CLOCKS_PER_SEC);

fclose(fd);
}

0 comments on commit 7e4f600

Please sign in to comment.