As part of a high performance computing course I'm trying to speed up a naive string search in C as much as possible, and I'd like to know if there's anything obvious that could be done that I've missed in my current iteration or if there's a better direction to go in.
Some restrictions are that the pattern must be searched left to right, each character must be checked separately, and it needs to be based on a for loop.
So far I've reduced the time taken for a pattern of 999 As followed by a B in a text of 9,999,999 As followed by a B from ~18 seconds to ~9 seconds, but I'm not sure if it could be faster given the restrictions above.
The current code is:
int match(char** text, int n, char** pattern, int m){
int i, j, last = n-m;
for(i = 0; i <= last; i++){
for(j = 0; j < m; j++){
if((*text)[i+j] != (*pattern)[j]){
break;
}
}
if(j == m){
return i;
}
}
return -1;
}
Where text is the text to be searched, pattern is the pattern to be searched, n is the length of the text, and m is the length of the pattern.
Edit: This is an implementation of the naive string search algorithm. https://en.wikipedia.org/wiki/String_searching_algorithm#Na.C3.AFve_string_search
Edit 2: Code after @RadLexus idea to change from char** to char* (25% faster):
int match(char* text, int n, char* pattern, int m){
int i, j, last = n-m;
for(i = 0; i <= last; i++){
for(j = 0; j < m; j++){
if(text[i+j] != pattern[j]){
break;
}
}
if(j == m){
return i;
}
}
return -1;
}