Busted
Busted

Reputation: 147

How to read huge file into vector or array

I made code that read file as vector. But it's terribly slow.(it takes 12sec to read about 430000lines) What's wrong with my code? When I do same thing in C# it takes only 0.5 ~ 1 sec.

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

using namespace std;

bool getFileContent(string fileName, vector<string> &vecOfStrs)
{
    ifstream in(fileName.c_str());

    if (!in)
    {
        std::cerr << "Cannot open the File : " << fileName << std::endl;

        return false;
    }

    string str;

    while (getline(in, str))
    {
        if (str.size() > 0)
        {
            vecOfStrs.push_back(str);
        }
    }

    in.close();

    return true;
}

int main()
{
    string my_file_path = "C:/Users/user/Desktop/myfile.txt";
    vector<string> lines;
    bool result = getFileContent(my_file_path, lines);

    if (result)
    {
        cout << lines.capacity() << endl;
    }
}

Upvotes: 2

Views: 513

Answers (1)

Loc Tran
Loc Tran

Reputation: 1180

I assumed you are using Visual Studio for developing your application. Do the following steps for Optimzation is /O2

1> Project Property --> Configuration Properties --> C/C++ --> Code Generation --> Basic Runtime Checks = Default

2> Project Property --> Configuration Properties --> C/C++ --> Optimization --> Optimization = Maximum Optimization (Favor Speed) (/O2)

It will make your program is optimized at run time at maximum level. If it still not good, I think you should count the number of line in file with this link How to count lines of a file in C++?

and reserve this number for initial vector's capacity.

Hope it will resolve your issue. Here is my solution

ifstream in("result_v2.txt");
vector<string> lines;
string str;

while (std::getline(in, str))
{
    if (str.size() > 0)
    {
        lines.push_back(str);
    }
}

in.close();

Upvotes: 3

Related Questions