[C ++ Anagram解算器速度优化

问题描述 投票:2回答:1

我决定为我的父亲制作一个字谜解算器。我对编程很陌生,但我认为我仍然可以做到。我的成品可以用,但速度确实很慢,例如找到8个字符的所有组合大约需要15分钟以上。我正在寻找对其进行优化/使其更快的方法。

与MinGW c ++编译器一起使用,在Clion 2019.3.4上,CPU:i7 9700k,RAM:16GB / 3200mhz。

#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

//Menu for interacting with user, not really important
void menu() {
    cout << "=========================" << endl;
    cout << "======== !WOW! ==========" << endl;
    cout << "=========================" << endl;
    cout << "1 ... INSERT" << endl;
    cout << "2 ... PRINT" << endl;
    cout << "3 ... LIMIT WORD LENTGH" << endl;
    cout << "4 ... NEW GAME" << endl;
    cout << "0 ... EXIT" << endl;
    cout << "=========================" << endl;
    cout << "Select: ";
}

//Function to find all possible combinations from letters of a given string 
void get(vector<string> &vec, string str, string res) {

    vec.push_back(res);

    for (int i = 0; i < str.length(); i++)
        get(vec, string(str).erase(i, 1), res + str[i]);
}

//Only for testing purposes
void printVec(vector<string> vec) {
    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
}

//Function to check if a given word exists in given .txt file
bool checkWord(vector<string> &vec2, string filename, string search) {

    string line;
    ifstream myFile;
    myFile.open(filename);

    if (myFile.is_open()) {
        while (!myFile.eof()) {
            getline(myFile, line);
            if (line == search) {
                vec2.push_back(line);
                return true;
            }
        }
        myFile.close();
    } else
        cout << "Unable to open this file." << endl;

    return false;
}


int main() {

    int selection;
    bool running = true;
    string stringOfChars;
    vector<string> vec;
    vector<string> vec2;

    do {

        menu();
        cin >> selection;
        switch (selection) {

            case 1:
                cout << "Insert letters one after another: ";
                cin >> stringOfChars;
                get(vec, stringOfChars, ""); //fill first vector(vec) with all possible combinations.
                break;

            case 2:
                for (int i = 0; i < vec.size(); i++) {
                    if (checkWord(vec2, "C:/file.txt", vec[i])) { //For each word in vector(vec) check if exists in file.txt, if it does, send it in vector(vec2) and return true
//Reason for vec2's existence is that later I want to implement functions to manipulate with possible solutions (like remove words i have already guessed, or as shown in case 3, to limit the word length)
                        cout << vec[i] << endl; //If return value == true cout the word
                    }
                }
                break;

            case 3:
                int numOfLetters;
                cout << "Word has a known number of letters: ";
                cin >> numOfLetters;
                for (int i = 0; i < vec2.size(); i++) { /*vec2 is now filled with all the answers, we can limit the output if we know the length of the word */
                    if (vec2[i].length() == numOfLetters) {
                        cout << vec2[i] << endl;
                    }
                }
                break;

            case 4:
                vec.clear();
                vec2.clear();
                break;

            case 0:
                running = false;
                break;

            default:
                cout << "Wrong selection!" << endl;
                break;
        }
        cout << endl;
    } while (running);

    return 0;
}


file.txt用我的语言填写了所有单词,按字母顺序排列,大小为50mb。

aachecnska
aachenskega
aachenskem
aachenski
.
.
.
bab
baba
babah
.
.
.

任何建议或主题外的提示都会有所帮助。我的想法之一是可能将file.txt分成较小的文件,例如将具有相同起始字母的行放在自己的文件中,因此A.txt仅包含以A开头的单词,等等。相应的代码。

c++ performance puzzle anagram
1个回答
1
投票

这是您需要使用profiler的位置。在Linux上,我最喜欢的是kcachgrind

http://kcachegrind.sourceforge.net/html/Home.html

它为您提供逐行计时信息,并告诉您应该最优化代码的哪一部分。

当然,有许多可用的探查器,包括商业探查器。

© www.soinside.com 2019 - 2024. All rights reserved.