Time to generate output

I have this program that searches through a text file for every word that starts with a certain prefix. But I need to put in a function that calculates how long it takes to find all the words in the text file. How would I do that? Here is my code...

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <list>
#include "Autocomplete.h"
using namespace std;


void Autocomplete:: sort_weight ()
{
	node_2 * temphead = beginning;
	//ListNode * tempnode = NULL;
	int temproll;
	string tempname;
	int counter = 0;
	while (temphead )
	{
		temphead = temphead->after;
		counter++;
	}
	temphead = beginning -> after;
	
	if (counter == 1)
	{
		cout << "There is no search results match your query.  Please try again." << endl;
		return;
	}
	for (int j=0; j<counter; j++)
	{
		while (temphead->after)  //iterate through list until next is null
		{
			if (temphead->digits < temphead->after->digits)
			{
				/*tempnode = temphead;
				temphead = temphead->next;
				temphead->next = tempnode;*/
				temproll = temphead->digits;
				temphead->digits = temphead->after->digits;
				temphead->after->digits = temproll;

				tempname = temphead->txt;
				temphead->txt = temphead->after->txt;
				temphead->after->txt = tempname;
				temphead = temphead->after;//increment node
			}
			else 
				temphead = temphead->after;//increment node
		}	
		temphead = beginning;//reset temphead
	}
}


void Autocomplete :: search(string txt, string prefix, int num, int result, int position)
{
	Autocomplete sort;
	for(int i = 1; i <= prefix.size(); i++)
	{
		if((prefix.substr(i - 1, 1) == txt.substr(i,1)) && result >= 0)
		{
			continue;
		}
		else
		{
			return;
		}
	}
	sort.InsertAtEnd_2(num,txt,position - prefix.size());
	
}

void Autocomplete :: CreateList_2(string file)
{
	//delclare node temp.
	node_2 *temp;
	//initialize temp
	temp = new(node_2); 
	//set text(which was declared in class) to file.
	temp -> txt = file;
	temp -> after = NULL;

	//If start of list equals NULL enter statement.
	if (beginning == NULL)
	{
		temp -> before = NULL;
		//set beginning of list to temp.
		beginning = temp;
	}
	//if not enter else statement.
	else
	{
		//enter loop if the term after start is not NULL.
		while (beginning -> after != NULL)
		{
			//set start equal to line proceeding it.
			beginning = beginning -> after;
			//set that line equal to temp.
			beginning -> after = temp;
			//set previous line equal to start.
			temp -> before = beginning;
		}
	}
	//End function and return too main.
}
//Creat function that insert something at end of file taking parameters file(content of file), and size(number of lines).
//This function will enter lines into list but reorder them since orginally it insert backwards and finally it will insert new line.
void Autocomplete :: InsertAtEnd_2(int num, string file, int size)
{
	//Declare nodes temp and temp_2.
	node_2 *temp, *temp_2;
	//set temp_2 to beginning of list(start).
	temp_2 = beginning;


	//if you have not reached the end of the file enter loop and do not exit till you reach end of file.
	for (int i = 0; i < size; i++)
	{
		//progress through loop.
		temp_2 = temp_2 -> after;
	}
	//initialize temp.
	temp = new(node_2);
	temp -> txt = file;
	temp -> digits = num;
	int count = 0;

	//if line following q equals NULL enter

	if (beginning -> after == NULL)
	{
		//progress q and set equal to tmp.
		temp_2 -> after = temp;
		//set line following tmp to NULL.
		temp -> after = NULL;
		//set line Proceeding tmp to temp_2.
		temp -> before = temp_2;      
	}

	//if not enter if statement
	else
	{
		//set line following tmp equal to line following temp_2.
		temp -> after = temp_2 -> after;
		temp -> after -> before = temp;
		temp_2 -> after = temp;
		temp -> before = temp_2;
	}
}
void Autocomplete :: print_2()
{
	//declare and initialize line_number.
	int line_number = 1;
	//declare node temp_2
	node_2 *temp_2;
	//set equal to start or beginning of list
	temp_2 = beginning;
	//if first line is equal to blank space enter statement
	if( temp_2->txt == "")
	{
		//proceed to followinf line skipping over this plan line
		temp_2 = temp_2-> after;
	}

	//enter loop if temp_2 != null (have not reached end of file)
	while (temp_2 != NULL && temp_2-> txt != "")
	{
		//output line number and each line in the list till you reach the end
		cout << temp_2 -> digits << " " << temp_2 -> txt << endl;
		//progress to next line
		temp_2 = temp_2 -> after;
		//increment line number
		line_number += 1;
	}
	
}

void Autocomplete :: Delete(string file)
{
	//declare nodes temp and set equal to start which is declared in the class LinkedList. So it starts from beginning.
	struct node_2* temp = beginning;
	if(temp->txt == "" || temp->after->txt == "/tWal-Mart Stores")
	{
		return;
	}
	//start for loop to run through current linked list, from beginning, until you reach specified line to delete(pos).
	while(temp->after->txt != "")
	{
		//temp = temp -> after;
		//temp = beginning;
		//progress through linked list by setting temp(current line) equal to the proceeding line in the linked list.
		//when done temp will equal line proceeding line specified by user.
		node_2* temp_2 = temp -> after;
		temp-> after = temp_2 ->after;
		free(temp_2);
		//if(temp -> after -> txt == "")
		//{
		//	free(temp);
		//	beginning == NULL;
		//	beginning -> after == NULL;
		//	//beginning -> after -> after = NULL;
		//	return;
		//}

	} 
	//
	

}


int main()
{
	
	string file;
	int num;
	string file_name;
	ifstream infile;
	int size = 0;
	int largest_pos = 0;
	string prefix;
	int results = 0;
	Autocomplete sort;
	int position = 0;
	//call function to create list
	sort.CreateList_2(file);
	
	//ask user for file to open and store in variable
	/*cout << "What file would you like to open: ";
	getline(cin, file_name);*/
	infile.open("cities_sorted.txt");
	
	//check if file name is valid
	if(infile.fail())
	{
		//if not display error message and end program
		cout <<"The file cannot be opened, terminating program!" << endl;
		system("pause");
		exit(0);
	}

	
	while(prefix != "Q" || prefix != "q")
	{
		sort.Delete(file);
	
		cout <<"Please enter query: ";
		getline(cin, prefix);
		//cin >> results;
		
		while(infile >> num && (getline(infile, file)))
		{
			sort.search(file, prefix, num, results, position);
			//increase size
			size += 1;
			//increase largest_pos
			largest_pos += 1;
		
		}
	
		sort.sort_weight();
		sort.print_2();
	}

	//txt_file.sort_weight();
	//txt_file.print(file);
	cout << endl << endl << endl;
	

	//call function to print current file to screen
	//txt_file.print(file_name);
	system("pause");
	return 0;
}
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
#include <iostream>
#include <ctime>
#include <chrono>

int main()
{
    const auto real_start = std::chrono::steady_clock::now() ;
    const auto cpu_start = std::clock() ;

    // **************************************************************
    // call the function to be timed (place the code to e timed) here
    // **************************************************************

    const auto cpu_end = std::clock() ;
    const auto real_end = std::chrono::steady_clock::now() ;

    // compute processor time used
    const auto cpu_milliseconds = (cpu_end-cpu_start) * 1000.0 / CLOCKS_PER_SEC ;

    // compute elapsed real time
    using namespace std::chrono ;
    const auto real_milliseconds = duration_cast<milliseconds>(real_end-real_start).count() ;

    std::cout << "processor: " << cpu_milliseconds << " milliseconds.\n"
              << "     real: " << real_milliseconds << " milliseconds.\n" ;
}
Thank you so much!
Topic archived. No new replies allowed.