I have a tailq with 100k+ entries of this struct:
struct entry {
char *file_name;
FILE *file;
TAILQ_ENTRY(entry) tailq;
};
The purpose is store thousands of file pointers for an application creates thousands of files and append stuffs to them.
On each increase of the tailq I have a foreach:
int c;
char temp[20];
struct entry *np;
TAILQ_FOREACH(np, &tailq_head[y], tailq) {
if(strcmp(np->file_name, temp) == 0){
c = 1;
break;
}
}
That searches for some temp name that already is on the tailq, if it isn’t in the tail then add id, else do not.
What can I do to improve performance? What is the faster structure that I can use? Should I calculate an integer hash to the temp variable to be compared in the foreach? Ideas?
Keeping an integer hash of the name in each entry will speed up the compare by a significant amount. It will also save one level of pointer indirection. But you’re still comparing against every single entry. If you store the entries in a structure that provides for efficient search without comparing against every single entry, like a hash table, the performance benefit would be even greater.