0

Working on a project where I have to return immediate children of queried line.A query can either be the first or second property. The input file is the output of pre-order traversal in an n-array tree. Each line can be thought of as a vertex in a n -array tree. How can this be improved performance-wise. Format of input file:

Level|Property1|Property2
1|AAAAAAAAAAAAAA|BBBBBBBBBBBBBBBBBBB
2|VVVVVVVVVVVVVV|fdgvjds sdbsks sdds
3|3784bbubsd@kjn|ejijo eurugvie
3|wiuefhiewbf844|weubwfbwe ewj
4|yyyyyyyyyyyyyy|wjfb wufwwfw
4|uuuuuuuuuuuuuu|wewb wrwrw
4|tttttttttttttt|webi wewewef
2|qqqqqqqqqqqqqq|yuweuw wsdjsods
If i query for AAAAAAAAAAAAAA, I get -> 2|VVVVVVVVVVVVVV|fdgvjds sdbsks sdds
If i query for fdgvjds sdbsks sdds, I get -> 3|3784bbubsd@kjn|ejijo eurugvie
                                             3|wiuefhiewbf844|weubwfbwe ewj
If i query for wiuefhiewbf844, I get -> 4|yyyyyyyyyyyyyy|wjfb wufwwfw
                                       4|uuuuuuuuuuuuuu|wewb wrwrw
                                       4|tttttttttttttt|webi wewewef
If i query for qqqqqqqqqqqqqq, I get -> NIL

The property Level, is what determines immediate children. This is the code I used. I have only inserted the constructor of my class. Any variables not initialized in it are defined as private. I am using a vector output to store the result.

    Reader(const char *Filename,string query)
    {
        ifstream in(Filename, ios::in | ios::binary);
        string contents;
        in.seekg(0, ios::end);
        contents.resize(in.tellg());
        in.seekg(0, ios::beg);
        in.read(&contents[0], contents.size());
        in.close();
        istringstream ss(contents);
        string line;
        int flag = 0,searchLevel;
        while(getline(ss,line,'\n'))
        {
            istringstream ss(line);
            string token, Property1, Property2;
            getline(ss, token, '|');
            int level = convertToInteger(token);
            int i = 1;
            while (getline(ss, token, '|'))
            {
                if (i == 1)Property1 = token;
                if (i == 2)
                {
                    Property2 = token;
                    break;
                }
                i++;
            }
            if (!flag)
            {
                if (query == Property1 || query == Property2)
                {
                    flag = 1;
                    searchLevel = level;
                }
            }
            else if (level <= searchLevel)break;
            else if (searchLevel+1 == level)
                    output.push_back(line);
        }
    }

How can I improve speed of my code?

child
  • 23
  • 4
  • this looks pretty good https://stackoverflow.com/questions/3910326/c-read-file-line-by-line-then-split-each-line-using-the-delimiter if you replace `\t` with `|` – SSpoke Jun 11 '20 at 09:15
  • Thanks! parsing line by line from the file is where most time is spent.Any thoughts on to improve that? There are some answers which use boost or that require unix based libraries: https://stackoverflow.com/questions/17925051/fast-textfile-reading-in-c. – child Jun 17 '20 at 06:15
  • `std::stringstream` from that question link i posted is the fastest way to do what you are trying to do.. boost library won't make it any faster.. it might even make it a little slower. If you want to make it as fast as possible.. i would pre-load into a cache all the data.. with the correct datatype formats.. this way you can avoid re-loading the data every loop in the while() loops. Then just use `lambda` with a custom comparison function to get exactly what you want to achieve. It's slow right now because you have `2 while()` loops if you get rid of 1.. it will be 50% faster. – SSpoke Jun 17 '20 at 08:21

0 Answers0