hi i was wondering if someone can give me some ideas of how to reduce the time it takes for the program to search 1 million words with a list of 850 words.
thanks
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
usingnamespace std;
time_t begin, end;
void main()
{
time(&begin);
//opens file cotaining words that need to be removed
ifstream WordList("WordList.txt");
ifstream Original("word.txt");
char str[15];
int max = 10;
string* words = new string[max];
int x= 0;
int y = 0;
//read text file holding list of all the words
while(!WordList.eof())
{
//adding all the words held onto a dynamic array
WordList.getline( str, 15);
words[x] = str;
x++;
//if array is too small make it larger and copy over to
if( x >= max)
{
max = max *2;
string* temp = new string[max];
for(int i = 0; i < x; i++)
{
temp[i] = words[i];
}
delete [] words;
words = temp;
}
}
//reset max back to 10 for file that is being read in
//same as for word array above this
max = 10;
string* readin = new string[max];
while( !Original.eof())
{
Original >> str;
readin[y] = str;
y++;
if( y >= max)
{
max = max *2;
string* temp2 = new string[max];
for(int i = 0; i < y; i++)
{
temp2[i] = readin[i];
}
delete [] readin;
readin = temp2;
}
}
WordList.close();
Original.close();
int WordSize = x;
int ReadinSize = y;
cout << endl;
//uses loops to check if file in read in text file
//contains words needed to be removed
bool check = false;
ofstream Final("dest.txt");
for( int i = 0; i <= ReadinSize; i++)
{
check = false;
for(int x = 0; x <= WordSize; x++)
{
if(readin[i] == words[x])
{
check = true;
}
}
if( check == false)
{
Final << readin[i] << endl;
cout << readin[i] << endl;
}
}
Final.close();
//deallocate memeory used in the array
delete[] words;
delete[] readin;
time(&end);
cout << "Time elapsed: " << difftime(end, begin) << " seconds"<< endl;
system("pause");
}
Consider using a std::deque<std::string> to hold the list of words read from Original. This will reduce the number of allocations you need considerably. Consider also sorting the words so that you can binary search it.