#include #include #include #include #include #include #include using namespace std; class timer { public: timer() : start_timing(hi_res_clock::now()) {} void reset() { start_timing = hi_res_clock::now(); } double elapsed() const { return(std::chrono::duration_cast (hi_res_clock::now() - start_timing).count()); } private: typedef std::chrono::high_resolution_clock hi_res_clock; typedef std::chrono::duration > second_; std::chrono::time_point start_timing; }; void print_time(const string & heading, const double & t) { cout << heading << " : "; cout.setf(ios::right); cout.setf(ios::showpoint); cout.setf(ios::fixed); cout.width(10); cout.precision(6); cout << t << endl; } using namespace std; int main() { string heading; const int nwords = 375086; typedef std::set words; words dictionary; string word; double t; timer timer_01; timer timer_02; heading = "\nProgram starts"; t = timer_01.elapsed(); print_time(heading, t); char file_name[] = "words"; ifstream input_file(file_name); if (!input_file) { cerr << " Error in opening the file" << endl; cerr << " Expecting to find a file called \"words\" " << endl; exit(EXIT_FAILURE); } const int buffer_size = 80; char buffer[buffer_size]; words::iterator j; for (int i = 1; i < nwords; ++i) { input_file.getline(buffer, buffer_size - 1); word = buffer; dictionary.insert(word); } heading = "\n Read \n"; t = timer_01.elapsed(); print_time(heading, t); timer_01.reset(); cout << "Type in a word "; cin >> word; t = timer_01.elapsed(); heading="\n User input\n"; print_time(heading, t); timer_01.reset(); j = dictionary.find(word); if (j != dictionary.end()) cout << " Word " << word << " in dictionary " << endl; else cout << " word not found " << endl; heading="\n Dictionary lookup\n"; t = timer_01.elapsed(); print_time(heading, t); timer_01.reset(); return(0); }