forked from koblas/Array-Intersection-Benchmark
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
David Koblas
committed
Mar 17, 2011
1 parent
b6df07a
commit 7e4f600
Showing
34 changed files
with
1,000,748 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Oops, something went wrong.