I have a program that reads in a dictionary file (1 word per line) and load it into a hash table as fast as possible, currently I'm using mmap() to read in the whole file then to parse it I just use a loop to check every single character and if that character is a '\n' then we store it in the hash table.
My question is there any way I can do this any faster by using other functions or by improving my code, I've tried fgets and fscanf but mmap seems to be the fastest.
Here's the gist of my code
int fp = open(file, O_RDONLY, S_IRUSR | S_IWUSR);
struct stat sb;
if (fstat(fp, &sb) == -1) {
perror("couldn't get file size.\n");
}
char *text = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fp, 0);
char word[46];
int temp_index = 0;
for (int i = 0; i < sb.st_size; i++) {
if (text[i] == '\n') {
temp[temp_index] = '\0';
int index = hash(word);
continue;
}
temp[temp_index] = text[i];
temp_index ++;
}
Sample dictionary.txt
home
house
phone
stack
overflow