optimisation of the program to reduce time of program

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

#include <iostream>
#include <string>
#include <fstream>
#include <time.h>

using namespace 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.


Topic archived. No new replies allowed.