patterncppMinor
Improving speed of word search algorithm
Viewed 0 times
searchalgorithmwordspeedimproving
Problem
I've been working on this word search algorithm and had some issues about its speed. It kinda looks like it takes a little too much time.
If anyone could offer suggestions on making this better and improving my skills, that would be great.
```
#include
#include
#include
using namespace std;
bool Check(string Letter, string Matrix)
{
return (Letter == Matrix) ? true : false;
}
int main()
{
int Height = 0, Widht = 0, Numb = 0, Longer = 0, Look = 0, Look1 = 0, Look2 = 0, Look3 = 0, Poz = 0, Poz1 = 0;
int Loop = 0, Loop1 = 0, Loop2 = 0, Loop3 = 0, Loop4 = 0, Loop5 = 0, Loop6 = 0;
bool Found, Found1, Found2, Found3;
ifstream Data("Files/Info.txt");
Data >> Widht >> Height;
string Matrix[Height][Widht];
while(Loop > Matrix[Loop][Loop1];
Loop1++;
}
Loop++;
}
Data >> Numb;
string Words[Numb];
while(Loop2 > Words[Loop2];
Loop2++;
}
Data.close();
while(Loop3 < Numb)
{
Longer = Words[Loop3].size();
Loop4 = 0;
Loop6 = 0;
cout << Words[Loop3] << endl;
while(Loop4 < Height) // Looks horizantaly
{
Loop5 = 0;
Look = 0;
Look1 = 0;
while(Loop5 < Widht - Longer + 1)
{
Found = Check(Words[Loop3].substr(Look, 1), Matrix[Loop4][Loop5]);
if(Found)
{
Loop6 = 1;
Look = Loop5;
Poz = Loop4;
Poz1 = Loop5;
while(Loop6 < Longer)
{
Look++;
Found1 = Check(Words[Loop3].substr(Loop6, 1), Matrix[Loop4][Look]);
if(Found1)
If anyone could offer suggestions on making this better and improving my skills, that would be great.
```
#include
#include
#include
using namespace std;
bool Check(string Letter, string Matrix)
{
return (Letter == Matrix) ? true : false;
}
int main()
{
int Height = 0, Widht = 0, Numb = 0, Longer = 0, Look = 0, Look1 = 0, Look2 = 0, Look3 = 0, Poz = 0, Poz1 = 0;
int Loop = 0, Loop1 = 0, Loop2 = 0, Loop3 = 0, Loop4 = 0, Loop5 = 0, Loop6 = 0;
bool Found, Found1, Found2, Found3;
ifstream Data("Files/Info.txt");
Data >> Widht >> Height;
string Matrix[Height][Widht];
while(Loop > Matrix[Loop][Loop1];
Loop1++;
}
Loop++;
}
Data >> Numb;
string Words[Numb];
while(Loop2 > Words[Loop2];
Loop2++;
}
Data.close();
while(Loop3 < Numb)
{
Longer = Words[Loop3].size();
Loop4 = 0;
Loop6 = 0;
cout << Words[Loop3] << endl;
while(Loop4 < Height) // Looks horizantaly
{
Loop5 = 0;
Look = 0;
Look1 = 0;
while(Loop5 < Widht - Longer + 1)
{
Found = Check(Words[Loop3].substr(Look, 1), Matrix[Loop4][Loop5]);
if(Found)
{
Loop6 = 1;
Look = Loop5;
Poz = Loop4;
Poz1 = Loop5;
while(Loop6 < Longer)
{
Look++;
Found1 = Check(Words[Loop3].substr(Loop6, 1), Matrix[Loop4][Look]);
if(Found1)
Solution
-
-
Some generalities regarding naming convention:
Variables and functions should start with a lowercase letter:
Custom types should start with a capital letter:
Macros should be uppercase:
-
Its parameters should also be passed by constant reference since they're not being modified:
-
You should make sure the input file is open before proceeding. If not, terminate from
-
You could vastly improve readability by using more functions. Putting everything in
In its current state, more comments may help others follow the code. That, in turn, could help flesh out any optimization issues. However, this isn't a substitute for writing readable code.
widht is misspelled; it should be width. :-)-
Some generalities regarding naming convention:
Variables and functions should start with a lowercase letter:
// "camel case"
int camelCaseVariable;
void camelCaseFunction() {}// "snake case"
int snake_case_variable;
void snake_case_function() {}Custom types should start with a capital letter:
class Class;Macros should be uppercase:
#define MACRO 0-
Check()'s body can be simplified as such:return Letter == Matrix; // already returns true or falseIts parameters should also be passed by constant reference since they're not being modified:
bool Check(string const& Letter, string const& Matrix) {}-
You should make sure the input file is open before proceeding. If not, terminate from
main() by returning 1 or EXIT_FAILURE.-
You could vastly improve readability by using more functions. Putting everything in
main() is not always a good idea, especially with a lot of nesting. Define the functions to perform one important task, and call them in main() or wherever else as needed. Function call overhead shouldn't hurt you here, making the readability aspect more important.In its current state, more comments may help others follow the code. That, in turn, could help flesh out any optimization issues. However, this isn't a substitute for writing readable code.
Code Snippets
// "camel case"
int camelCaseVariable;
void camelCaseFunction() {}// "snake case"
int snake_case_variable;
void snake_case_function() {}class Class;#define MACRO 0return Letter == Matrix; // already returns true or falseContext
StackExchange Code Review Q#33681, answer score: 4
Revisions (0)
No revisions yet.