Created
September 30, 2014 22:02
-
-
Save tierra/141b6f875149dc0f297e to your computer and use it in GitHub Desktop.
tierra's C++ vs dataw0lf's Python speedhash challenge!
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
// Kill me if I ever have to use glibc ever again, almost as bad as Perl. | |
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <search.h> | |
int main (int argc, char* argv[]) | |
{ | |
ENTRY e, *ep; | |
FILE *hash_fd, *list_fd; | |
// This will need to be increased to fit the list sadly. | |
char* wordlist[100000]; | |
char* hashfile = argv[1]; | |
char* wordfile = argv[2]; | |
char* word = NULL; | |
size_t wordsize = 0; | |
int bytes_read = 0, i; | |
unsigned int count = 0, match_count = 0; | |
hash_fd = fopen(hashfile, "r"); | |
if(hash_fd == NULL) { | |
fprintf(stderr, "Error opening hash file.\n"); | |
return 1; | |
} | |
while((bytes_read = getline(&word, &wordsize, hash_fd)) != -1) { | |
wordlist[count] = strtok(word, "\n"); | |
count++; | |
word = NULL; | |
wordsize = 0; | |
} | |
close(fileno(hash_fd)); | |
hcreate(count); | |
for(i = 0; i < count; i++) { | |
e.key = wordlist[i]; | |
e.data = (char*)i; | |
ep = hsearch(e, ENTER); | |
if (ep == NULL) { | |
fprintf(stderr, "Hash entry failed.\n"); | |
return 1; | |
} | |
} | |
list_fd = fopen(wordfile, "r"); | |
if(list_fd == NULL) { | |
fprintf(stderr, "Error opening word file.\n"); | |
return 1; | |
} | |
while((bytes_read = getline(&word, &wordsize, list_fd)) != -1) { | |
e.key = strtok(word, "\n"); | |
if(hsearch(e, FIND)) | |
match_count++; | |
word = NULL; | |
wordsize = 0; | |
} | |
close(fileno(list_fd)); | |
printf("Matching Lines: %d\n", match_count); | |
return 0; | |
} |
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
#define _GNU_SOURCE | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
#include <string> | |
#include <map> | |
using namespace std; | |
int main (int argc, char* argv[]) | |
{ | |
map<string, bool> tehmap; | |
FILE *hash_fd, *list_fd; | |
char* hashfile = argv[1]; | |
char* wordfile = argv[2]; | |
char* word = NULL; | |
string stlword; | |
size_t wordsize = 0; | |
int bytes_read = 0, i; | |
unsigned int match_count = 0; | |
printf("Initialized variables, opening list of words to add to hash table...\n"); | |
hash_fd = fopen(hashfile, "r"); | |
if(hash_fd == NULL) { | |
fprintf(stderr, "Error opening hash file.\n"); | |
return 1; | |
} | |
printf("Adding words from file to hash table...\n"); | |
while((bytes_read = getline(&word, &wordsize, hash_fd)) != -1) { | |
stlword = strtok(word, "\n"); | |
tehmap[stlword] = true; | |
word = NULL; | |
wordsize = 0; | |
} | |
close(fileno(hash_fd)); | |
printf("Opening list of words to check against hash table...\n"); | |
list_fd = fopen(wordfile, "r"); | |
if(list_fd == NULL) { | |
fprintf(stderr, "Error opening word file.\n"); | |
return 1; | |
} | |
printf("Checking words against entries in the hash table...\n"); | |
while((bytes_read = getline(&word, &wordsize, list_fd)) != -1) { | |
stlword = strtok(word, "\n"); | |
if(tehmap.find(stlword) != tehmap.end()) | |
match_count++; | |
word = NULL; | |
wordsize = 0; | |
} | |
close(fileno(list_fd)); | |
printf("Matching Lines: %d\n", match_count); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment