C++快速读取大文件

debug的时候需要等很长时间读模型,查资料发现了两种快速读取大文件的方法。

test 1:每次读一个字符串

test 2、3一次读取整个文件

    {//test 1
        string buf;
        clock_t start = clock();
        ifstream fin(objpath);
        
        while (fin >> buf);
        
        fin.close();
        clock_t end = clock();
        cout << "time : " << ((double)end - start) / CLOCKS_PER_SEC << "s\n";
    }
    {//test 2
        clock_t start = clock();
        ifstream fin(objpath, std::ios::binary);
        
        vector<char> buf(fin.seekg(0, std::ios::end).tellg());
        fin.seekg(0, std::ios::beg).read(&buf[0], static_cast(buf.size()));
        
        fin.close();
        clock_t end = clock();
        cout << "time : " << ((double)end - start) / CLOCKS_PER_SEC << "s\n";
    }
    {//test 3
        clock_t start = clock();
        ifstream fin(objpath);

        stringstream buf;
        buf << fin.rdbuf();

        fin.close();
        clock_t end = clock();
        cout << "time : " << ((double)end - start) / CLOCKS_PER_SEC << "s\n";
    }

文件大小为112M,花费的时间分别为:

time : 36.809s
time : 0.092s
time : 4.213s

于是将loader改成了第二种。

 

转载于:https://www.cnblogs.com/redips-l/p/8258306.html

你可能感兴趣的:(C++快速读取大文件)